If variables are boxes for holding data, then Python's collection data types are like warehouses that can hold many boxes. These warehouses have the following key properties:
- They can be ordered or unordered.
- They can be changeable or unchangeable.
When a collection is ordered this means that the data type will remember the order in which elements were added to it, and preserve this order. On the other hand, an unordered collection will not maintain the order in which elements are added to it.
A changeable collection is a collection that can be modified. This means elements can be added to or removed from it after it has been created. An unchangeable collection is one that cannot be modified after its creation.
Lists are an ordered and changeable data type. Here is an example of a basic list containing three strings:
>>> my_list = ["string-1", "string-2", "string-3"]
>>> print(my_list)
['string-1', 'string-2', 'string-3']
Lists are defined by writing square brackets. Any elements in the list would then be placed inside the brackets, and separated by a comma. But elements are actually optional, and it's fine to create an empty list to start with.
>>> my_empty_list = []
>>> print(my_empty_list)
[]
When we access a particular item within our list, this is known as indexing. To do this, we must place some square brackets at the end of the list and provide a number that corresponds with the location of the item we wish to retrieve. Python uses zero-based indexing and so the item at index 0 would be the first element in the list, the item at index 1 would be the second element in our list, and so-on. An example is given below.
>>> my_list = ["string-1", "string-2", "string-3"]
>>> print(my_list)
['string-1', 'string-2', 'string-3']
>>> my_list[0]
'string-1'
If we attempt to access an item in our list that doesn't exist, we get an IndexError. If a list contains three items then Python doesn't know what to do when we ask for element four.
>>> my_list[3]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IndexError: list index out of range
Because lists are changeable, we're allowed to add / remove elements from our list. The append()
command allows us to add an item to the end of the list, while the remove()
command allows us to take something away from a list.
>>> my_list.append("string-4")
>>> print(my_list)
['string-1', 'string-2', 'string-3', 'string-4']
>>> my_list.remove("string-4")
>>> print(my_list)
['string-1', 'string-2', 'string-3']
Note that remove()
takes away only the first occurrence of an item.
>>> my_list
['string-1', 'string-2', 'string-3']
>>> my_list.append("string-2") # adding the same item again
>>> my_list
['string-1', 'string-2', 'string-3', 'string-2']
>>> my_list.remove("string-2")
>>> my_list
['string-1', 'string-3', 'string-2']
The insert()
command also allows adding an item to a list at a particular index.
>>> my_list = ["string-1", "string-2", "string-3"]
>>> my_list.insert(2, "hello")
>>> my_list
['string-1', 'string-2', 'hello', 'string-3']
pop()
can be used to remove an item at a particular index.
>>> my_list
['string-1', 'string-2', 'hello', 'string-3']
>>> my_list.pop(2)
'hello'
>>> my_list
['string-1', 'string-2', 'string-3']
We're also allowed to use the +
operator to combine lists.
>>> my_list + ["string-4"]
['string-1', 'string-2', 'string-3', 'string-4']
As lists are changeable, we may also take the data at a particular index in our list and replace it with something else.
>>> my_list
['string-1', 'string-2', 'string-3']
>>> my_list[1] = "lists are changeable"
>>> my_list
['string-1', 'lists are changeable', 'string-3']
clear()
allows us to remove everything from a list.
>>> my_list
['string-1', 'lists are changeable', 'string-3']
>>> my_list.clear()
>>> my_list
[]
Tuples are an ordered and unchangeable data type. Tuples are created by using round brackets.
>>> my_tuple = ("hello-1", "hello-2", "hello-3")
>>> my_tuple
('hello-1', 'hello-2', 'hello-3')
Like in the case of lists, we can use indexing to access a particular item from a Tuple.
>>> my_tuple
('hello-1', 'hello-2', 'hello-3')
>>> my_tuple[2]
'hello-3'
Because Tuples are unchangeable, we cannot add or remove items from them, or change the data at a given index.
>>> my_tuple[0] = "tuples are not changeable"
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'tuple' object does not support item assignment
We can use the +
operator like with we did with Lists, but that is because we're actually creating a new Tuple rather than changing an existing one.
>>> my_tuple + (1,)
('hello-1', 'hello-2', 'hello-3', 1)
Dictionaries are an ordered (since Python 3.7) and changeable data type. Dictionaries are comprised of key-value pairs that map one piece of data to another.
To create a dictionary we use curly brackets. Then any key-value pairs are placed inside these curly brackets, and separated my commas. A colon is used to define a key-value pair.
>>> capitals = {"United Kingdom": "London", "Azerbaijan": "Baku", "Norway": "Oslo"}
>>> capitals
{'United Kingdom': 'London', 'Azerbaijan': 'Baku', 'Norway': 'Oslo'}
Here we use square brackets to access an item in our Dictionary, but instead we provide a key rather than an index.
>>> capitals["Norway"]
'Oslo'
Adding information to a dictionary is then as simple as assigning a value to a particular key.
>>> capitals["Peru"] = "Lima"
>>> capitals
{'United Kingdom': 'London', 'Azerbaijan': 'Baku', 'Norway': 'Oslo', 'Peru': 'Lima'}
The del
keyword can be used to remove data from our dictionary.
>>> del capitals["United Kingdom"]
>>> capitals
{'Azerbaijan': 'Baku', 'Norway': 'Oslo', 'Peru': 'Lima'}
We can also change the value for an existing key.
>>> capitals["Norway"] = "Cairo"
>>> capitals
{'Azerbaijan': 'Baku', 'Norway': 'Cairo', 'Peru': 'Lima'}
Now let's change that back to the right value, and add Egypt to the Dictionary.
>>> capitals["Norway"] = "Oslo"
>>> capitals["Egypt"] = "Cairo"
>>> capitals
{'Azerbaijan': 'Baku', 'Norway': 'Oslo', 'Peru': 'Lima', 'Egypt': 'Cairo'}
Trying to access data that does not exist in the Dictionary leads to a KeyError.
>>> capitals
{'Azerbaijan': 'Baku', 'Norway': 'Oslo', 'Peru': 'Lima', 'Egypt': 'Cairo'}
>>> capitals["South Korea"]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
KeyError: 'South Korea'
Dictionary keys have to be unique. Attempting to have two bits of data with the same key will simply result in one value overwriting the other.
Sets are an unordered and changeable data type. They are a special type of Collection that prevent repeating elements. Sets are also defined using curly brackets.
>>> my_set = {1, 2, 3}
>>> my_set
{1, 2, 3}
However, an empty pair of curly brackets makes Python create a Dictionary.
>>> my_set
{1, 2, 3}
>>> type(my_set) # check the type of our set
<class 'set'>
>>> not_a_set = {} # this is not a set
>>> type(not_a_set) # type tells us this is actually an empty dictionary
<class 'dict'>
If we want to create an empty set we have to use the set()
command instead.
>>> actually_a_set = set() # create an empty set the proper way
>>> type(actually_a_set) # type tells us this is a set
<class 'set'>
We can try to add a repeating item to our Set and Python won't give an Error, but it will simply refuse to add that item.
>>> my_set
{1, 2, 3}
>>> my_set.add(2)
>>> my_set
{1, 2, 3}
A similar thing applies to creating Sets.
>>> my_set = {1, 2, 3, 3} # create a set with a duplicate 3
>>> my_set
{1, 2, 3}
Strings aren't a type of collection, but can still behave like them. For example, we can use indexing to get a particular character from a string.
>>> my_string = "hello"
>>> my_string[3]
'l'
Strings are also unchangeable.
>>> my_string[3] = "!"
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'str' object does not support item assignment
Slicing allows us to pick segments from a List or String. Let's say we wanted to get just the first three characters from a String or the first three elements from a list. Slicing makes it much simpler to do this.
>>> my_string = "hello"
>>> my_string[:3]
'hel'
>>> my_list = [1, 2, 3, 4, 5]
>>> my_list[:3]
[1, 2, 3]
By indexing at -1, we can pick out the last item.
>>> my_string
'hello'
>>> my_string[-1]
'o'
>>> my_list
[1, 2, 3, 4, 5]
>>> my_list[-1]
5
You can read more about slicing here.
It is possible to check for the length of a collection using len()
.
>>> len(["a", "b", "c"])
3
>>> len([]) # empty list
0
>>> len(("item-1", "item-2", "item-3"))
3
>>> len(()) # empty tuple
0
>>> len({'United Kingdom': 'London', 'Azerbaijan': 'Baku', 'Norway': 'Oslo'})
3
>>> len({}) # empty dictionary
0
In the case of Dictionaries, len()
counts the number of key-value pairs.
>>> len({1, 2, 3})
3
>>> len(set()) # empty set
0
We can also observe that adding an item that already exists in a set does not cause its length to change.
>>> simple_set = {1, 2, 3}
>>> len(simple_set)
3
>>> simple_set.add(2)
>>> len(simple_set)
3
len()
also lets us see the length of a string.
>>> len("cat")
3
>>> len("")
0
The in
keyword in Python allows us to see if something is a member of a collection.
>>> my_list = ["a", "b", "c"]
>>> "a" in my_list
True
>>> "z" in my_list
False
>>> my_tuple = ("a", "b", "c")
>>> "a" in my_tuple
True
>>> "z" in my_tuple
False
In the case of Dictionaries, in
checks for the presence of a key.
>>> capitals = {'United Kingdom': 'London', 'Azerbaijan': 'Baku', 'Norway': 'Oslo'}
>>> "United Kingdom" in capitals
True
>>> "Singapore" in capitals
False
We cannot do the same thing with values. Checking if "Oslo" is in
our capitals
Dictionary gives back False
.
>>> "Oslo" in capitals
False
However, we could extract the values from our Dictionary and check if "Oslo" is one of them.
>>> capitals.values()
dict_values(['London', 'Baku', 'Oslo'])
>>> "Oslo" in capitals.values()
True
Likewise, a similar command exists for extracting the keys from a Dictionary.
>>> countries = capitals.keys()
>>> countries
dict_keys(['United Kingdom', 'Azerbaijan', 'Norway'])
>>> my_set = {1, 2, 3}
>>> 1 in my_set
True
>>> 100 in my_set
False
For strings, in
lets us see if one string contains another.
>>> college = "london college of communication"
>>> "london" in college
True
>>> "llege of" in college
True
>>> "utah" in college
False
We would say that "london" and "llege of" are substrings of "london college of communication".
- Collections in Python are like warehouses for our variable-boxes.
- Lists are ordered, changeable sequences defined with
[ ]
. - Lists support indexing, slicing, appending, removing, and concatenating.
- Tuples are ordered and unchangeable sequences defined with
( )
. Tuples also support indexing. - Dictionaries are ordered and changeable key-value pairs defined with
{ }
. - Dictionary values are accessed by keys, and dictionaries support adding, removing, and updating items.
- Sets are unordered, changeable collections with unique elements defined with
{ }
. - Sets prevent duplicates and support adding and removing.
- Strings aren't strictly a Collection but kind of act like one. They are an unchangeable sequences of characters, and support indexing and slicing.
- Slicing allows selecting a range of elements.
len()
: Returns the length of a collection.in
: Checks membership in a collection, including substring presence for strings.
Prev | List of Contents | Next