One big advantage of learning a new programming language from scratch is that it has helped identify a few gaps in my knowledge.

I’ve recently completed the section on Tuples, Lists and Dictionaries from the book ‘Python by Examples’. I’m already familiar with Lists and Dictionaries, but Tuples are something I’ve never had to use before. I felt that this was a great opportunity to learn something new for both C# and Python.

Lists, Dictionaries and Tuples all store collections of data. However, unlike lists and dictionaries, a tuple is unchangeable. Once implemented it cannot be altered.

Creating Tuples

Here, I’ve created a tuple that contains a collection of variables of different types.

Python:

In python, creating a tuple is actually pretty easy. All items within the tuple are contained within a pair of round brackets. This is then assigned to a variable. In this example, I’ve chosen to create tuples containing different types of variables.

random = (352, "France", 6.236, "Sports", 'r')

Accessing an item in the tuple is even easier. Just type the name of the variable along with an index number inside a pair of square brackets. A range of index numbers can be provided to print multiple tuple items.

#print items 0 and 3
print(random[0]) 
print(random[3])

#prints items 1 to 3 in the tuple
print(random[1:3])

To get the index number of a specific item in the tuple, we use the index() method.

#print the index number of an item in the tuple
selection = input("Which item would you like the index number of? ")
print(random.index(selection))
C#:

I found 2 methods for creating a tuple in C#. The first involves creating the Tuple and then declaring the variable type of each item in that tuple.

var t = new Tuple<char, string, char, double, int>('g', "Buckingham Palace", '5', 26.236, 52);
Console.WriteLine(t);

This is particular difficult and prone to error. Fortunately there is a less tedious method.

var t2= Tuple.Create("Spain", 26, "Maths", 't', 1);
Console.WriteLine(t2);

To access items within the tuple, we use item properties (Item 1, Item2 …)

Console.WriteLine(t.Item1);
Console.WriteLine(t2.Item4);

Here is the output for the C# code:

(g, Buckingham Palace, 5, 26.236, 52)
(Spain, 26, Maths, t, 1)
g
t

Nested Tuples (C# only)

In python, there is no limit to the number of items in the tuple. C# limits size to 8. If more than 8 items are required then more can be added by creating a nested tuple (a tuple inside another tuple).

Here I have created a Tuple which includes 2 nested tuples:

var t= Tuple.Create("Spain", 26, Tuple.Create("Burgers", 52), 't', "Harry Potter", "Picasso", Tuple.Create("Time", 6, 4, "Alice in Wonderland" , 'g'), 1);
Console.WriteLine(t);

Output:

(Spain, 26, (Burgers, 52), t, Harry Potter, Picasso, (Time, 6, 4, Alice in Wonderland, g), 1)

The additional tuples have been nested within items 3 and 7. So to access them, we need to use the Item properties (Item3 or Item7) then another Item property for the item number within the nested tuple.
(Please let me know if this didn’t make sense).

There is no Item property for item 8, so to access this you need to use the Rest property instead.

Console.WriteLine(t.Item1); //print item 1
Console.WriteLine(t.Rest); //print item 8

Console.WriteLine(t.Item3); //print item 3 - entire nested tuple
Console.WriteLine(t.Item3.Item2); //print item 2 from tuple item 3

Console.WriteLine(t.Item7.Item4); //print item 4 from tuple item 7

Output:

Spain
(1)
(Burgers, 52)
52
Alice in Wonderland

List of Tuples

I came across a C# example of a list of tuples being implemented. Here is an example of this being done. The item properties can still be used to access individual tuple items within the list. In this example, I only print the 1st item within each tuple.

var list = new List<Tuple<int, string>>();

list.Add(new Tuple<int, string>(1, "UK"));
list.Add(new Tuple<int, string>(2, "France"));
list.Add(new Tuple<int, string>(3, "Germany"));
list.Add(new Tuple<int, string>(4, "Spain"));
list.Add(new Tuple<int, string>(5, "Italy"));
list.Add(new Tuple<int, string>(6, "Austria"));

foreach(var listItem in list)
{
    Console.WriteLine(listItem.Item1);
}

Here I’ve implemented a similar example using Python. In this version, I’ve printed off the 2nd item in each tuple (remember, in Python the first item starts at 0 so 1 refers to the 2nd item):

list = []
list.append((1, "UK"))
list.append((2, "France"))
list.append((3, "Germany"))
list.append((4, "Spain"))
list.append((5, "Italy"))
list.append((6, "Austria"))

for i in list:
    print(i[1])

Comparing C# and Python

In C#, Tuples are complicated. However, if someone with programming experience but no knowledge of C#, they could probably work out what is going on. The difference between Lists and Tuples are easy to identify.

It is much easier to implement and access tuples in Python. However, by creating simpler code that is easier to implement, the code is less understandable.

In case of Lists and Tuples, the only difference in the code between Lists and Tuples are the type of brackets used.

  • Lists are contained within square braskets […]
  • Tuples are contains within round brackets (…)

It could be easy to get these mixed up. I’ve had to stick a post-it note on the wall next to my computer so I don’t forget. At least in C#, it is clearly stated in the code when we are working with a List or Tuple.

Common Uses

As mentioned before, I’ve never had to use tuples before. They can be used when none of the items need to change. One example that I’ve seen suggested is menu lists – these do not need to change while the program is being run.

Can anyone provide me with some real world examples where they’ve used tuples?

Further Information
C# Tuple Examples – this provided some great explanations and examples of how tuples are used in C#

I will be publishing more blog posts about my experiences while learning Python. Click here to see some of my blog posts that have already been published.

I’m currently working my way through the 150 challenges from Python By Example by Nichola Lacey. You can see the work I’ve done so far on my GitHub repository: https://github.com/LouiseJGibbs/Python-Exercises.