Skip to content

Lists, Tuples, Dictionaries, and Sets

Collections in Python allow you to store multiple bits of data in a single place.

Lists

Lists are a collection in Python. These are the most basic of the collections. Each item in the list is given an index which can be used to access each item in the list. These indexes start at 0. To create a list, you use put the items you want inside [] and separate each item with a comma. You can have duplicates in a list, as well as different data types in a list.

int_list = [1, 2, 3, 4]
str_list = ["tomato", "cherry", "strawberry"]
mixed_list = [True, 3.14, "lettuce", 5, 3.14]

You can access the items in the list by using indexes and you can add more items to the list using .append(). .append() will add the given item to the end of the list.

str_list = ["tomato", "cherry", "strawberry"]
# This will get the second item in the list, as indexes start at 0
cherry = str_list[1]
# This will change the item at index 0, "tomato", and make it "raspberry"
str_list[0] = "raspberry"
# This will add "apple" to the list after "strawberry"
str_list.append("apple")

You can also loop through lists. There are 2 main ways of doing so: using a for loop with range, or using a for loop with the list itself.

num_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
even_total = 0
# len() will give the number of items in the list and we use this with range to loop through all items in the list
for i in range(len(num_list)):
# Here we access the index using the variable i
if num_list[i] % 2 == 0:
even_total += num_list[i]
num_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
even_total = 0
# Here we access each item in the list and assign it to the variable num
for num in num_list:
# Now we can use num directly without needing to index each time
if num % 2 == 0:
even_total += num

Tuples

Tuples are similar to lists. The 2 main differences are that they use () instead of [], and once you have created the tuple you cannot change, add, or remove items from it.

str_tuple = ("tomato", "cherry", "strawberry")
# This will get the second item in the list, as indexes start at 0
cherry = str_tuple[1]
# This will not work as you cannot change the items in the tuple
str_tuple[0] = "raspberry"
# This will not work as you cannot add items to the tuple
str_tuple.append("apple")

Looping through tuples works just the same as with lists.

num_tuple = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
even_total = 0
# len() will give the number of items in the tuple and we use this with range to loop through all items in the tuple
for i in range(len(num_tuple)):
# Here we access the index using the variable i
if num_tuple[i] % 2 == 0:
even_total += num_tuple[i]
num_tuple = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
even_total = 0
# Here we access each item in the tuple and assign it to the variable num
for num in num_tuple:
# Now we can use num directly without needing to index each time
if num % 2 == 0:
even_total += num

Tuples are best used when you are working with data you do not want to accidentally change in some way.

Dictionaries

Dictionaries, or Dicts, use something called key-value pairs to store data. A key is a unique piece of data in the dictionary that is assigned a value. You cannot have duplicate keys, but you can have duplicate values. Dictionaries use {} instead of [] or (), and each key value pair is denoted by key:value. Each pair is separated by a comma.

book = {
"title" : "The Hunger Games",
"author" : "Suzanne Collins",
"first_printing" : 2008,
"second_printing" : 2008
}

Accessing dictionaries is different to lists and tuples. You don’t access the items by index, nor do you access the value directly. You need to use the key to access the values. You can also add key-value pairs to a dictionary by using a non-existent key.

book = {
"title" : "The Hunger Games",
"author" : "Suzanne Collins",
"first_printing" : 2008,
"second_printing" : 2008
}
# This will give the value associated with the key "title". In this case it is "The Hunger Games"
title = book["title"]
# This will add a key called "protagonist" to the dictionary with the value "Katniss Everdeen"
book["protagonist"] = "Katniss Everdeen"

Looping through dictionaries is also a bit different. for loops will give the dictionary’s keys when looped through.

book = {
"title" : "The Hunger Games",
"author" : "Suzanne Collins",
"first_printing" : 2008,
"second_printing" : 2008
}
# This will print each of the keys in the dictionary
for key in book:
print(key)
# This will print each value in the dictionary
for key in book:
print(book[key])

Sets

Sets are unordered, so to access items you need to know what item you want to access. You cannot have duplicate items in a set, but you can add and remove items. Sets use like dictionaries, but they don’t use key-value pairs.

str_set = {"tomato", "cherry", "strawberry"}
# This will get the item called "cherry" from the set
cherry = "cherry" in str_set
# This will remove the given item from the set, and return it.
removed_value = str_set.discard("tomato")
# This will add the item called "apple" to the set
str_set.add("apple")

Looping through sets works the same as the other collection types.

str_set = {"tomato", "cherry", "strawberry", "apple"}
# This will print each item in the set. Because it is unordered it might print in a different order each time.
for item in str_set:
print(item)