Lists, Tuples, Sets, and Dictionaries in Python
THIS ARTICLE IS STILL IN EDITING MODE
Contents
Introduction
In this tutorial, we will learn more about different ways to store data in PytHon. We will specifically look at lists, tuples, sets, and dictionaries (so-called compound data types) that can store multiple integers, strings, or even another set or tuple inside it. We will look at the advantages and disadvantages of the different compound datasets and when it's best to use which. When coding, it is hard to get around them since they are very useful when you clean and prepare your dataset.
Tuples
Tuples are an ordered sequence. As a sequence, it also has an index of its values, just like a string has an index for its characters. It is represented by **brackets** ().
Example: a Tuple with multiple data types has values “inside”
my_tuple = (”nagisa”, 7, 1.1) my_tuple[0] #”nagisa” my_tuple[1] #7 my_tuple[2] #1.1
To get the **length** of the tuple (how many elements inside a Tuple), we can use the function “len()”
Example:
len(my_tuple)
The unique feature of Tuple is that it is **immutable** which means that it is not changeable after it is created.
Example: if you don't believe me, you can try it
wrong_tuple = (1,2) wrong_tuple[0] = 2
Python will give you an error code since you have tried to change the tuple "wrong_tuple" by replacing the 1 with a 2.
Lists
Lists are also an ordered sequence, just like tuples.
The differences are: 1. It is represented by squared brackets [] 2. It is mutable, which means it can be changed after being created
Example: Changing the first element of a list
my_list = [”charmander”, 1, 1.2] my_list[0] = “charizard”
my_list now becomes [”charizard”, 1, 1.2]
Everything else is the same compared to tuples!
List - Aliasing
What happens when you create a list A = [”Goku”, 7] and then assign B = A ?
Try it yourself, what happened to List A when you change B[0] = “Vegeta”?
List A[0] will also changed to “Vegeta”.
When multiple names are referring to the same object, it is known as Aliasing.
List - Cloning
Sometimes, aliasing creates more problem than solving things. So remember, there is another method similar to liasing, but without referencing the same list, called Cloning.
To clone, simply use:
B = A[:]
The code above will clone List A, and name it B.
How to choose tuples or lists? Tuples and lists can be very similar in their function. However, some things need to be considered if you have to decide between lists and tuples. 1.) Order of the data: If the order of the data is very important, it is advisable to use tuples since they are immutable. This way you can guarantee, that the order does not change accidentally over time. If you want to change the order, you can just convert the tuple to a list, make the changes and convert it back. For example:
tuple_1 = (1, "A", 3, 4, 5) list=list(tuple_1) list[2]=6 tuple_1=tuple(list) tuple_1 (1, "A", 6, 4, 5)
But of course, converting it back and forth takes time and might be unhandy if you know that you might have to change your data compound from time to time. Then, a list might be better 2.) Data size and processing efficiency: Tuples are faster stored and processed than lists. Therefore, I might be useful to use tuples when you have a large dataset or know that other steps in your research demand a lot of time. For smaller datasets, it is not so important what you choose.
Nesting
In programming, nesting means having a group inside a group. Since tuples can contain tuples inside it as an element, it is called tuple nesting. You can imagine it as a folder inside of a folder.
Example: I want to save my biography as a tuple. It includes my name, my age, any 2 of my favourite colors as a tuple.
my_bio = (”Steph”, 22, (”Red”,”Orange”) )
How to access “Red” then? It can be accessed in 2 steps:
1. my_bio[2] → will give me (”Red”, “Orange”) 2. my_bio[2][0] → will give me “Red” specifically.
This means that I can choose the elements in my nested tuple with these squared brackets. The order of the brackets follow the same order as the nesting. The first bracket determines the element within the larger tuple, and the second one within the chosen tuple.
Dictionaries
Dictionaries are similar to lists, but it has unique indexes. Let’s start with an example of list and dictionary below:
List Index | List Element |
---|---|
0 | "Gohan" |
1 | "8" |
Dictionary Key | Header text |
---|---|
"name" | "Gohan" |
"age" | 8 |
As we can see above, instead of just numbers 0 and 1, dictionaries have a special unique “key”.
How to create Dictionary?
1. Use curly brackets {}. 2. The keys have to be immutable and unique 3. The values can be immutable, mutable, and duplicates (anything) 4. Each key and value pair is separated by a comma
Example:
my_dict = { ”num”: 1, “name”: “Bulma”, “children”: (”alice”, “batty”, “carry”), “age_children”:[4,5,6] }
However, above is just to give you an example of the flexibility of dictionary. Actually, in real world, people use dictionary, just like real-world people use dictionaries.
"Real Example"
my_phone_book = { “Chancellor”: 0123456789, “Beyonce”: 0987654321 }
So that you can just call the key : my_phone_book[“Chancellor”] or “Beyonce” to get their phone numbers.
Here are some special commands that might be useful to get you started using dictionaries: Dictionary special commands:
1. **del**: to delete an entry Example:
del(my_phone_book[’dad’])
→ it will delete the “dad” entry from the dictionary.
2. **in** : to verify if a key is in the dict Example:
‘mom’ in my_phone_book
3. **keys()** : method to get all the keys in the dict Example:
my_dict.keys()
4. **values()** : similar to keys(), but we get values instead Example:
my_dict.values()
Sets
Sets are a type of collections, just like lists and tuples. However sets only have unique elements.
Example:
my_set= {”goku”, “vegeta”, “krillin”, “**goku**”}
The duplicates will be automatically removed when the set is created.
Sets are therefore especially useful if you want to make sure that there are no duplicates in the data you work with. For example, if you want to know how many households there are with children, you can just create a set of all individuals in households with children, and the set will only choose one individual per household. You then know how many households with children there are.
Here are some additional commands to get you going using sets: 1. set() : to cast the list into a set. Example:
set(my_list)
2. add() : to add a new element into the set Example:
my_set.add(”majinbu”)
3. remove() : to remove an element in the set Example:
my_set.remove(”krillin”)
Mathematical operations:
1. AND (&) : intersection of two sets → find the similar element Example:
your_set = {”goku”, “naruto”, “natsu”} our_set = my_set & your_set → it will contain “goku”
2. union() Example:
my_set.union(your_set)
3. issubset() : to check if a set is a subset of a bigger set. Example:
small_set.issubset(big_set)
Quiz
1. Supposed you have a Tuple A = (11,22,33,44), print the last 2 element of the tuple. 2. What happens when you add 2 tuples together? tuple_1 = (”red”, 1) tuple_2 = (”blue”, 2) tuple_3 = tuple_1 + tuple_2 3. Is it possible to put Tuples inside Lists? How about the other way around? list = [ (”tuple”, 1), (”tuple”, 2) ] tuple = ([”list”, 1 ], [”list” , 2] ) How to access the first element of the list and tuple? 4. How to get the length of a set = {”A”,”B”,”C”} 5. You want to analyze how siblings interact with each other based on age difference and there relative position to their siblings (if they are the oldest/youngest child or in the middle). What kind of compound data would you use if you set up your data for the analysis?
The author of this entry is XX. Edited by Milan Maushart