Get startedGet started for free

What are the main data structures in Python?

1. What are the main data structures in Python?

Welcome to the course on Coding Interview Questions in Python! My name is Kirill Smirnov and I am a Data Science Consultant at Altran. We'll go through some common topics met during a job interview process. Let's start with the basic question: what are the main data structures in Python?

2. Data Structure

But first, what is a data structure? It is a specialized format to organize and store data. Python's standard library has four main data structures: list, tuple, set, and dictionary. Let's get through each of them!

3. List

List. List represents an ordered mutable sequence of items, like numbers, strings, and other objects. Lists are created by inserting comma-separated items into square brackets.

4. List: accessing items

To retrieve an item, we have to specify its index in square brackets after the variable's name. Remember, Python starts indexing from zero, not from one! Using negative indices is also possible. In this case, counting works backwards in the list. We can also access a sub-list via slicing. Note that the item corresponding to the right-hand side index is not included. Not specifying the left-hand or right-hand side index results in the whole range taken into account.

5. List: modifying items

Modifying items is very simple as well. We can change either a single item or a slice.

6. List: methods

Lists have some useful methods. .append() adds a new item to a list. .remove() deletes a specific item from a list.

7. List: methods

.pop() removes the last item from a list and returns its value. .count() counts the amount of a certain item in a list.

8. Tuple

Tuple. Tuple is an immutable sequence of items. Here, immutable means that we cannot modify it. There are two ways to create a tuple: Either by using round brackets or by writing comma-separated values. Accessing items in a tuple is similar to list mechanics.

9. Tuple: modifying values

Modifying though is not possible: we will get TypeError.

10. Set

Set. Set is an unordered collection with no duplicate items. Unordered means that there is no indexing for the constituent items. Here is the code to create a set. If we have duplicates during creation, they are not included.

11. Set: methods

Sets have some useful methods. .add() inserts a new item to a set. .remove() does the contrary. .union() returns a new set with items from both sets. .intersection() returns a new set with only common items. .difference() returns a new set with items present in one set but not in another.

12. Dictionary

Dictionary. Dictionary is a collection of key-value pairs where keys are unique and immutable. A key has a unique correspondence to its value but not vice versa. There are several ways to create a dictionary. For example, using curly brackets and specifying each key-value pair with a colon or using the dict() constructor together with a list of tuples.

13. Dictionary: accessing values

A value associated with a key can be accessed by specifying the key within square brackets. The operation raises KeyError if the key does not exist.

14. Dictionary: modifying values

Modifying the value for a key is very straightforward: Accessing the value and re-assigning it. If the key does not exist, the operation creates a new key-value pair.

15. Dictionary: methods

Dictionaries have some useful methods. .items() returns the stored key-value pairs.

16. Dictionary: methods

We can pass the method output to the list() constructor to get the corresponding list.

17. Dictionary: methods

We can also retrieve keys and values separately

18. Dictionary: methods

and also use the trick with the list() constructor.

19. Dictionary: methods

We can remove the last inserted key-value pair with the .popitem() method. The method also returns the associated value.

20. Operations on Lists, Tuples, Sets, and Dictionaries

We can apply some operations on all the aforementioned collections. One very practical is: len() that returns the collection's size.

21. Operations on Lists, Tuples, Sets, and Dictionaries

Another one uses the in keyword that checks if an item is already present in a collection.

22. Let's practice!

We went through some main data structures in Python. Now it's time to practice!