DEV Community

Cover image for Python data structures (lists, tuples, dictionaries, sets)
Thea
Thea

Posted on

Python data structures (lists, tuples, dictionaries, sets)

In this blog post, we are going to study a short overview of some data structures specific to Python3.

Python has four basic inbuilt data structures: Lists, Dictionary, Tuple, and Set. We will see how to use each of them and how they make life easier for us.

Python list

Lists in Python are collections of elements. They can be as long as you want, and the individual elements can have the same type or not. The items in a list are separated by commas and enclosed in square brackets:

some_list = [1, 2, 3, 4, 5]
some_list = ['a','b','c','d']
some_list = [7, 'hi', None, False, True, ['another', 'list']]
Enter fullscreen mode Exit fullscreen mode

To access an element in a list, we use bracket notation and pass in the index. Lists in Python use a zero-based index:

some_list = ['a', 1, False]
print(some_list[2]) #False
Enter fullscreen mode Exit fullscreen mode

In addition, there are a number of inbuilt methods you can use. Here are some more ones:

append() - adds a value to the end of a list:

some_list = [11,25,3]
some_list.append(10)
print(some_list) #[11,25,3,10]
Enter fullscreen mode Exit fullscreen mode

clear() - removes all the values in a list:

some_list = [1,2,3]
some_list.clear()
print(some_list) #[]
Enter fullscreen mode Exit fullscreen mode

copy() - makes a shallow copy of a list:

some_list = [2,3,4]
another_list = some_list.copy()
print(another_list) #[2, 3, 4]
Enter fullscreen mode Exit fullscreen mode

count() - counts how many times an item appears in a list:

some_list = [1,2,3,4,5,5,5,5]
some_list.count(1) #1
print(some_list.count(5)) #4
Enter fullscreen mode Exit fullscreen mode

extend() - adds all elements of a list to some other list:

some_list = [1,2,3]
some_list.extend([5,6,7])
print(some_list) #[1,2,3,5,6,7] 
Enter fullscreen mode Exit fullscreen mode

index() - returns the index of an element. If the element is not found, index returns a ValueError:

some_list = [4,8,6,4]
print(some_list.index(8)) #1
print(some_list.index(4)) #0 - it only finds the index of the first matching element
print(some_list.index(12)) #ValueError
Enter fullscreen mode Exit fullscreen mode

insert() - inserts an element to the list at the given index:

some_list = [4,6,8]
some_list.insert(2,'awesome') #inserts 'awesome' at index 2
print(some_list) #[4,6,'awesome',8]
Enter fullscreen mode Exit fullscreen mode

pop() - removes the last element from a list and returns the element removed:

some_list = [3,4]
print(some_list.pop()) #4
print(some_list.pop()) #IndexError
Enter fullscreen mode Exit fullscreen mode

pop() throws an error if you call it on an empty list.

remove() - removes the first occurrence of a value:

some_list = [1,2,3,1]
some_list.remove(1)
print(some_list) #[2,3,1]
Enter fullscreen mode Exit fullscreen mode

reverse() - reverses the order of all elements of the list:

some_list = [1,2,3,4]
some_list.reverse()
print(some_list) #[4,3,2,1]
Enter fullscreen mode Exit fullscreen mode

sort() - sorts all elements of a list in the ascending order:

some_list = [1,4,3,2]
some_list.sort()
print(some_list) #[1,2,3,4]
Enter fullscreen mode Exit fullscreen mode

Python Tuple

Similar to a list, a tuple in Python is also a collection of items. The differences are that the tuples are immutable, which means we can not change the elements once assigned, and use parentheses, whereas lists use square brackets.
Because of immutability, operations on tuples are faster than on lists.

some_tuple = ('a', 'b', 'c', 'd')
Enter fullscreen mode Exit fullscreen mode

To write a tuple containing a single value you have to include a comma, even though there is only one value:

single_tuple = ('a',)
Enter fullscreen mode Exit fullscreen mode

Following are some common methods used on tuples:

count() - returns the number of times a value appears in a tuple:

some_tuple = (1,2,3,3,3)
print(some_tuple.count(1)) #1
print(some_tuple.count(3)) #3
Enter fullscreen mode Exit fullscreen mode

index() - returns the index of the given element in the tuple:

some_tuple = (1,2,3,3,3)
print(some_tuple.index(1)) #0
print(some_tuple.index(5)) #ValueError
Enter fullscreen mode Exit fullscreen mode

Python Dictionary

Dictionaries in Python are key value pairs. They are defined using curly braces, separating key value pairs with a comma, and placing a colon between the key and the value:

weekdays = {
    1: 'Monday',
    2: 'Tuesday',
    3: 'Wednesday',
    4: 'Thursday',
    5: 'Friday',
    6: 'Saturday',
    7: 'Sunday'
}
Enter fullscreen mode Exit fullscreen mode

-keys in a dictionary must be unique (no two same keys)
-keys are immutable
-keys and values can be of any data type
-the keys() function returns a list of keys in a dictionary
-the values() function returns a list of values in a dictionary

Following are the common methods on dictionaries:

clear() - clears all the keys and values in a dictionary:

car = {
  'brand': 'Maserati',
  'model': 'Quattroporte'
}
car.clear()
print(car) #{}
Enter fullscreen mode Exit fullscreen mode

copy() - makes a copy of a dictionary:

car = {
  'brand': 'Maserati',
  'model': 'Quattroporte'
}

x = car.copy()
print(x) #{'brand': 'Maserati', 'model': 'Quattroporte'}
Enter fullscreen mode Exit fullscreen mode

fromkeys() - creates key value pairs from comma separated values:

keys = ('a', 'e', 'i', 'o', 'u')
value = 'vowel'

vowels = dict.fromkeys(keys, value) 
#{'a': 'vowel', 'u': 'vowel', 'o': 'vowel', 'e': 'vowel', 'i': 'vowel'}
Enter fullscreen mode Exit fullscreen mode

get() - retrieves a key in an object and returns None instead of a KeyError if the key does not exist:

car = {
  'brand': 'Maserati',
  'model': 'Quattroporte'
}

x = car.get('model')
print(x) #Quattroporte
Enter fullscreen mode Exit fullscreen mode

items() - returns a list of tuples with each key-value pair:

car = {
  'brand': 'Maserati',
  'model': 'Quattroporte'
}

x = car.items()
print(x) #[('brand', 'Maserati'), ('model', 'Quattroporte')]
Enter fullscreen mode Exit fullscreen mode

keys() - returns a dict_keys object containing all of the keys in an object:

car = {
  'brand': 'Maserati',
  'model': 'Quattroporte'
}

x = car.keys()
print(x) #(['brand', 'model'])
Enter fullscreen mode Exit fullscreen mode

setdefault() - returns the value of the specified key:

car = {
  'brand': 'Maserati',
  'model': 'Quattroporte'
}

x = car.setdefault('model', 'Ghibli')
print(x) #Quattroporte 
Enter fullscreen mode Exit fullscreen mode

update() - update keys and values in a dictionary with another set of key value:

car = {
  'brand': 'Maserati',
  'model': 'Quattroporte'
}

x = car.update({'color': 'white'})
print(x) #{'brand': 'Maserati', 'model': 'Quattroporte', 'color': 'white'}
Enter fullscreen mode Exit fullscreen mode

values() - returns a list of all the values in the dictionary:

car = {
  'brand': 'Maserati',
  'model': 'Quattroporte'
}
x = car.values()
print(list(x)) #['Maserati', 'Quattroporte']
Enter fullscreen mode Exit fullscreen mode

Python Set

A set is an unordered collection of objects in Python. In addition to being iterable and mutable, sets do not have duplicate values. They can be useful if you need to keep track of a collection of elements, but don't care about ordering.

Here are some set methods:

add() - adds an element to a set. If the element is already in the set, the set doesn’t change:

s = {1,2,3}
s.add(4)
print(s) #{1, 2, 3, 4}
s.add(4)
print(s) #{1,2,3,4}
Enter fullscreen mode Exit fullscreen mode

clear() - removes all the items of the set:

s = {1,2,3}
s.clear()
print(s) #set()
Enter fullscreen mode Exit fullscreen mode

copy() - creates a copy of the set:

s = {1,2,3}
another_s = s.copy()
print(another_s) #{1, 2, 3}
Enter fullscreen mode Exit fullscreen mode

difference() - returns a new set with all the elements that are in the first set but not in the set passed to difference:

set1 = {1,2,3}
set2 = {2,3,4}
x = set1.difference(set2) 
print(x) #{1}
y = set2.difference(set1) 
print(y) #{4}
Enter fullscreen mode Exit fullscreen mode

intersection() - returns a new set containing all the elements that are in both sets:

set1 = {1,2,3}
set2 = {2,3,4}
x = set1.intersection(set2) 
print(x) #{2,3}
Enter fullscreen mode Exit fullscreen mode

union - returns a union of two sets:

set1 = {1,2,3}
set2 = {2,3,4}
x = set1.union(set2) 
print(x) #{1,2,3,4}
Enter fullscreen mode Exit fullscreen mode

A good understanding of data structures is very important for developers. I hope this article will help you to understand data structures for your Python projects.

Top comments (0)