OpenSesame
Rapunzel Code Editor
DataMatrix
Support forum
Python Tutorials
MindProbe
Python videos

Iterables: list, dict, tuple, and set

This tutorial contains eight interactive mini exercises and two review exercises. Try to solve them all!

Iterables, order, and mutability

In Python, an iterable object (or simply an iterable) is a collection of elements that you can loop (or iterate) through one element at a time. Simply put, an iterable is a list-like (or array-like) object. There are many kinds of iterables, which differ in many ways, including whether they are ordered and mutable:

  • An iterable is ordered if you can retrieve its elements in a predictable order
  • An iterable is mutable if you can change which elements it contains

Python has four built-in iterable types: list, dict, tuple, and set

list: an ordered, mutable collection of elements

A list consists of zero or more other elements in a fixed order. The elements of a list can be anything, such a numbers (int), strings (str), and even other lists. Different elements from the same list can have different types.

A list is defined as a comma-separated list of values between square brackets:

prime_numbers = [1, 3, 5, 7, 11]
print(prime_numbers)
empty_list = []
print(empty_list)

Output:

[1, 3, 5, 7, 11]
[]

Mini exercise

Print a list that contains the first three letters of the alphabet as three separate elements.

Getting a single element (indexing)

To get a single element from a list, use the index (position) of the element, where 0 is the first element, 1 is the second element, etc. That is, Python uses zero-based indexing. You can also count from the end of the list by using a negative index, where -1 is the last element, -2, is the second-to-last element, etc.

print('First prime number:')
print(prime_numbers[0])
print('Last prime number (of our list):')
print(prime_numbers[-1])

Output:

First prime number:
1
Last prime number (of our list):
11

Mini exercise

Create a list that contains the first five prime numbers. Print out the second number from this list.

Getting a range of elements (slicing)

You can also get multiple elements from a list by specifying a slice.

letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g']
from_index = 0
to_index = 4 # Non-inclusive, so until and including 'd' (at index 3)
in_steps_of = 3
print(letters[from_index:to_index:in_steps_of])

Output:

['a', 'd']

All the parts of a slice are optional, and fall back to the start of the list (for from_index), the end of the list (for to_index), and steps of 1. So you can get the first two prime numbers by only specifying the to_index, like this:

print(prime_numbers[:2])

Output:

[1, 3]

A common way to create a copy of a list to get the full slice. (This is equivalent to list.copy() in Python 3.)

my_copy = prime_numbers[:]

Mini exercise

Again create a list that contains the first five prime numbers. Print out a slice that contains all numbers from this list except for the first and the last one.

len(): counting the number of elements

len() counts how many elements there are in a list, or more generally in any iterable that has a length. (Some special iterables have no length, for example because they correspond to an infinite series. However, all iterables that we consider here have a length.)

n = len(prime_numbers)
print('We have defined {0} prime numbers'.format(n))

Output:

We have defined 5 prime numbers

in: checking whether an iterable contains an element

Python has a special operator, in, to check whether an element is part of a list or another iterable.

if 3 in prime_numbers:
    print('Three is prime')

Output:

Three is prime

Mini exercise

Again create a list with the first five prime numbers. Ask the user for input. Convert this input to int, and check whether the resulting number is in our list of prime numbers. If it is, print out 'The number is prime', else print out 'The number is not among the first five prime numbers'. When running the code, enter one of the first five prime numbers to solve the exercise.

Modifying a list

A list has several common functions for adding or removing elements:

  • list.append(element) adds an element to the end
  • list.insert(index, element) inserts an element at position index
  • element = list.pop() removes the last element from the list and returns it. You can also specify an index of an element, in which case this element is removed and returned (instead of the last element).
prime_numbers = []
prime_numbers.append(1)
prime_numbers.append(3) # Etc.
print(prime_numbers)

Output:

[1, 3]

For an overview of list functions, see:

Mini exercise

Again create a list with the first five prime numbers. Remove the third number from the list, using either pop() or remove(). Print out the resulting list.

dict: an unordered, mutable collection of key-value pairs

A dict consists of zero or more key-value pairs. That is, each element of a dict is a combination of a value that serves as a key and that is associated with another value. A dict is unordered, which means that if you iterate through the elements of a dict, you are not guaranteed to get the elements in a particular order. So never rely on the order of a dict!

A dict supports len() and in, as described above for lists. However, indexing a dict works differently from a list, as discussed below, and slicing is not supported.

A dict is defined as a comma-separated list of key : value mappings between curly braces.

ages = {
    'Jay-Z': 52,          # and counting
    'Emanuel Macron': 44  # and counting
}
print(ages)

Output:

{'Jay-Z': 52, 'Emanuel Macron': 44}

Getting a single element (indexing)

If you want to get an elements from a dict, you do this by specifying a key.

age_jayz = ages['Jay-Z']
print(f'Jay-Z is {age_jayz} years old')

Output:

Jay-Z is 52 years old

Alternatively, you can use dict.get(), which allows you to specify a default value in case the key is not in the dict.

age_depp = ages.get('Johnny Depp', 'unknown')
print(f'Johnny Depp is {age_depp} years old')

Output:

Johnny Depp is unknown years old

Mini exercise

Define a dict of the three largest cities in the world, where keys are names and values are population numbers in millions of inhabitants (e.g. 37 for Tokyo). Next, ask the user to enter the name of a city. If the city name is part of the dict, print out the population in the following format: 'The population of X is Y million inhabitants'. If the city name is not part of the dict, fall back to using 'unknown' for the population value.

When running the code, enter 'Tokyo' to solve the exercise.

tuple: an ordered, immutable collection of elements

The tuple is very similar to a list. The main difference between the two types is that a tuple is immutable, which means that a tuple cannot be changed after it has been defined; in other words, there are no functions such as tuple.append(), tuple.pop(), etc. However, you can concatenate two tuples using the + operator, in which case you get a new tuple that contains the elements of both of the concatenated tuples.

A tuple supports indexing, slicing, len() and in, as described above for lists.

A tuple is defined as a comma-separated list of values, optionally surrounded by parentheses.

fibonacci = 1, 1, 2, 3, 5
# Parentheses are optional unless they are required to avoid ambiguity
fibonacci = (1, 1, 2, 3, 5)

Mini exercise

Create one tuple that contains the first five numbers of the fibonacci series. Create another tuple that contains the first five prime numbers. Concatenate the two tuples and print out the result.

set: an unordered, mutable collection of unique elements

The set is a somewhat unusual type, which you will rarely use unless you're doing mathematical computations. A set corresponds to a mathematical set as specified in set theory.

A set supports len() and in, as described above for lists. However, a set does not support indexing or slicing.

A set is defined as a comma-separated list of values surrounded by curly braces.

vowels = {'a', 'e', 'i', 'o', 'u'}

A characteristic feature of sets is that each element can occur at most once (duplicates are ignored):

print({'a', 'e'} == {'a', 'a', 'e', 'e'})

Output:

True

The set also supports standard operations from set theory:

  • The union (|) of two sets contains all elements that occur in either or both of the sets
  • The intersection (&) of two sets contains all elements that occur in both sets
vowels_1 = {'a', 'e', 'i'}
vowels_2 = {'i', 'o', 'u'}
print('Union: {0}'.format(vowels_1 | vowels_2))
print('Intersection: {0}'.format(vowels_1 & vowels_2))

Output:

Union: {'u', 'a', 'i', 'o', 'e'}
Intersection: {'i'}

For more information, see:

Mini exercise

Create one set that contains the first five numbers of the fibonacci series. Create another set that contains the first five prime numbers. Print out all numbers that occur in both sets (i.e. the intersection).

Review exercises

Fibonacci

Define a fibonacci list that corresponds to the Fibonacci series up to 8. Then, use slicing to create two subseries:

  • even_fibonacci, with all numbers at even indices (0, 2, etc.); and
  • odd_fibonacci, with all numbers at odd indices (1, 3, etc.).

This exercise is not checked automatically, because there are several possible solutions. Click here to see one solution!

Best-selling artists

Define an artists dict with the names of the four best-selling music artists as keys, and their claimed sales as values:

  1. The Beatles with 600 million sales
  2. Elvis Presley with 600 million sales
  3. Michael Jackson with 350 million sales
  4. Madonna with 300 million sales

Then ask the user to enter the name of an artist. Look up the number of sales of this artist, falling back to 'unknown' if the artists is not in the dict, and print out the result.

This exercise is not checked automatically, because there are several possible solutions. Click here to see one solution!

You're done with this section!

Continue with Loops >>