Skip to content

#4: Lists, Dictionaries, Sets & Tuples

This week I learned more about the different data structures in Python. In the second try I got a better understanding on the subtle differences in creating collections. Collections are an important way to group data and move it around and I hope this post will help me to keep them apart.

Lists

Lists are a mutable collection of elements. You define a list using **[]** and separate the elements by a comma:

list = ['a', 'b', 'c', 'd']

You can select the different elements using a 0-based index:

>>> list[2]
'c'

The same is possible using an index starting at the end of the list:

>>> list[-1]
'd'

I hope this image explains how those indexes change by the direction you want to access the elements: Index positions in Python

Your lists can contain various datatypes side-by-side:

list = ['A', 34, 5.6, "blue"]

Since lists are mutable, you can change a list by adding new elements or removing them:

1
2
3
4
5
6
7
>>> list.append(6)
>>> list
['A', 34, 5.6, 'blue', 6]

>>> list.remove(34)
>>> list
['A', 5.6, 'blue', 6]

We can reverse the elements in a list:

1
2
3
>>> list.reverse()
>>> list
[6, 'blue', 5.6, 'A']

Tuples

Tuples look like lists, but they are immutable and cannot be changed after you created them. You create a tuple using **()** and separate the elements by a comma:

tuple = (4, 5, 7)

You can skip the () and just use the commas:

tuple = 4, 5, 6

The example above is called tuple packing, in which the numbers 4, 5 and 6 are packed together. You can reverse this (called sequence unpacking) and assign each element to a different variable:

1
2
3
4
5
6
7
>>> x, y, z = tuple
>>> x
4
>>> y
5
>>> z
6

Dictionaries

Dictionaries are a collection of key/value pairs that have no particular order. You can create a dictionary by using **{}** and separate the key from its value with a colon:

dictionary = {'a' : 44949, 9: "Hello"}

You get the value for a key by using the index operation:

>>> dictionary[9]
'Hello'

You can replace an existing value by assigning a new value to that key. If this key does not exist, a new entry is added:

1
2
3
4
5
6
7
>>> dictionary[9] = "changed"
>>> dictionary
{'a': 44949, 9: 'changed'}

>>> dictionary['c'] = "new Value"
>>> dictionary
{'a': 44949, 9: 'changed', 'c': 'new Value'}

Sets

To show why I had problems with keeping the different structures apart I like to talk about sets as the last structure of this post. Sets are unordered, mutable collections with no duplicate elements. You create sets like a list, but use **{}** instead of []:

my_set = {1,2,3,4,2,3}

You can specify duplicated elements when you create your set, but the set will not add them:

>>> my_set
{1, 2, 3, 4}

You can add more elements using add(), but it will ignore your addition when the element is already present:

1
2
3
4
5
6
7
>>> my_set.add(6)
>>> my_set
{1, 2, 3, 4, 6}

>>> my_set.add(6)
>>> my_set
{1, 2, 3, 4, 6}

As with lists, we can change our set by adding new elements or removing them:

1
2
3
4
5
6
7
>>> my_set.add(6)
>>> my_set
{1, 2, 3, 4, 6}

>>> my_set.remove(4)
>>> my_set
{1, 2, 3, 6}

Warning: You cannot create an empty set using {}, then that creates a dictionary:

>>> empty = {}
>>> type(empty)

To create an empty set, you need to call the function set() without any arguments:

>>> empty = set()
>>> type(empty)

Next

There are many more built-in data types and structures that I need to understand. I will cover them as I learn more about Python. The next step for me is to learn more about strings.