Data Structures in Python
In this article, we'll take a closer look at the four built-in data structures in Python: lists, tuples, dictionaries, and sets. Each of these data structures has its own distinct characteristics and use cases, and you'll learn how to create and perform basic operations on each of them.
Lists
Lists are ordered collections of items that can be of any data type, including strings, integers, floats, and other lists. Lists are defined using square brackets and items are separated by commas. Lists can be mutable, meaning their elements can be added, removed, or modified after they are created.
my_list = [1, 2, 3, 4, 5]
- You can access each item in a list by its index, which is a zero-based index that starts at 0. For example,
my_list[0]
would return1
.
- You can extract a subset of items from a list using slicing. For example,
my_list[1:3]
would return the second and third items in the list.
- To add an item to the end of a list use the
append()
method:my_list.append(6)
would add the number 6 to the end of the list.
- You can insert an item at a specific position in the list using the
insert()
method:my_list.insert(1, 5)
would insert the number 5 at the second position in the list.
- You can remove the first occurrence of an item in the list using the
pop()
method. For example,my_list.pop(0)
would remove the first item in the list.
- In order to remove all items from the list use the
clear()
method.
- You can sort the items in a list using the
sort()
method (ascending order is the default order). To sort the list in descending order usemy_list.sort(reverse=True)
.
- To reverse the order of the items in a list use the
reverse()
method.
For more details about Lists in Python, read our article "Everything About Lists in Python".
Tuples
Tuples are similar to lists, but they are immutable, meaning their elements cannot be modified after they are created. If you try to modify an element of a Tuple, you will get a TypeError
.
Tuples are defined using parentheses () and items are separated by commas.
my_tuple = (1, 2, 3, 4, 5)
- You can access the elements of a Tuple using indexing, just like with a list:
tup = (1, 2, 3)
andprint(tup[0])
would print1
.
- You can slice a Tuple just like a list. For example,
tup = (1, 2, 3, 4, 5)
andprint(tup[1:3])
would print(2, 3)
.
- You can perform basic arithmetic operations on Tuples, such as addition and multiplication. For example,
tup1 = (1, 2)
andtup2 = (3, 4)
andprint(tup1 + tup2)
would print(4, 6)
.
- You can use comparison operators to compare Tuples element-wise. For example,
tup1 = (1, 2)
andtup2 = (3, 4)
andprint(tup1 > tup2)
would printTrue
.
- You can use the
len()
function to find the length of a Tuple:tup = (1, 2, 3)
andprint(len(tup))
would print3
.
For more details about Tuples in Python, read our article "Everything About Tuples in Python".
Sets
Sets are unordered collections of unique items, and they can contain only unique items. Sets are defined using curly braces {} and items are separated by commas.
my_set = {1, 2, 3, 4, 5}
You can add items to a set using the add()
method:
my_set = {1, 2, 3}
my_set.add(4)
print(my_set) # [1, 2, 3, 4]
You can remove items from a set using the discard()
method:
my_set = {1, 2, 3, 4}
my_set.discard(2)
print(my_set) # [1, 3, 4]
You can combine the elements of two sets using the union()
method:
set1 = {1, 2, 3}
set2 = {3, 4, 5}
result = set1.union(set2)
print(result) # {1, 2, 3, 4, 5}
Dictionaries
Dictionaries are unordered collections of key-value pairs, where each key is unique and maps to a specific value. Dictionaries are defined using curly braces {} and keys are separated from values using a colon (:).
my_dict = {"foo": "bar", "number": 42, "hallo": "World"}
- Accessing a value: You can access the value associated with a key by using the key in the dictionary. For example:
print(my_dict['foo'])
will print "bar".
- Updating a value: You can update the value associated with a key by using the
=
operator:my_dict['number'] = 123
will update the value of the key "number" to123
.
- Inserting a new key-value pair: You can insert a new key-value pair into the dictionary using the
=
operator:my_dict['DE'] = 'de.jberries.com'
will insert a new key-value pair with the key "DE" and the value "de.jberries.com".
- Deleting a key-value pair: You can delete a key-value pair from the dictionary by using the
del
keyword. For example:del my_dict['foo']
will delete the key-value pair with the key "foo".