Understanding Data Structures in Python: Lists, Tuples, and Dictionaries

Data Structures in Python
Facebook
Twitter
LinkedIn
Reddit

What Is a Data Structure?

A data structure is a way of organizing data in computer memory, implemented in a programming language. It is a fundamental concept as data structures are one of the main building blocks of any modern software. Learning what data structures exist and how to use them efficiently in different situations is one of the first steps toward learning any programming language.

Data Structures in Python
Data Structures in Python

Data Structures in Python

Built-in data structures in Python can be divided into two broad categories: mutable and immutable. 

  • Mutable data structures are those we can modify — for example, by adding, removing, or changing their elements. Python has three mutable data structures: listsdictionaries, and sets
  • Immutable data structures, on the other hand, are those that we cannot modify after their creation. The only basic built-in immutable data structure in Python is a tuple.

Python also has advanced data structures, such as stacks or queues, which can be implemented with basic data structures. However, these are rarely used in data science.

Let’s start with the mutable data structures: lists, dictionaries, and sets.

  • Lists

Lists in Python are implemented as dynamic mutable arrays which hold an ordered collection of items.

  • Arrays are data structures that contain a collection of elements of the same data types (for instance, all elements are integers). 
  • lists can contain heterogeneous data types and objects. For instance, integers, strings, and even functions can be stored within the same list. 
  • Different elements of a list can be accessed by integer indices where the first element of a list has the index of 0.
  • lists are ordered, which means they retain the order in which you insert the elements into the list.
  • we can arbitrarily add, remove, and change elements in the list. 
  • The .append() method adds a new element to a list, and the .remove() method removes an element from a list. Furthermore, by accessing a list’s element by index, we can change it to another element.
  • When creating a list, we do not have to specify in advance the number of elements it will contain; therefore, it can be expanded as we wish, making it dynamic.

Lists are useful when we want to store a collection of different data types and subsequently add, remove, or perform operations on each element of the list (by looping through them). Furthermore, lists are useful for storing other data structures (and even other lists) by creating, for instance, lists of dictionaries, tuples, or lists. It is very common to store a table as a list of lists (where each inner list represents a table’s column) for subsequent data analysis.

    pros of lists are:

  • They represent the easiest way to store a collection of related objects.
  • They are easy to modify by removing, adding, and changing elements.
  • They are useful for creating nested data structures, such as a list of lists/dictionaries.

   Cons of lists are:

  • They can be pretty slow when performing arithmetic operations on their elements. (For speed, use NumPy’s arrays.)
  • They use more disk space because of their under-the-hood implementation.

Examples:

let’s have a look at a few examples.

We can create a list using either square brackets ([]) with zero or more elements between them, separated by commas, or the constructor list(). The latter can also be used to transform certain other data structures into lists.

We can access a list’s elements using indices, where the first element of a list has an index of 0:

We can also slice lists and access multiple elements simultaneously:

Note- that we did not have to specify the index of the last element we wanted to access if we wanted all elements from index 2 (included) to the end of the list. Generally speaking, the list slicing works as follows:

  1. Open square brackets.
  2. Write the first index of the first element we want to access. This element will be included in the output. Place the colon after this index.
  3. Write the index, plus one of the last elements we want to access. The addition of 1 here is required because the element under the index we write will not be included in the output.
  • lists are mutable

For example, we can append() a new element to a list or remove() a specific element from it:

Additionally, we can modify the elements that are already in the list by accessing the required index and assigning a new value to that index:

Dictionaries

  • Dictionaries in Python are very similar to real-world dictionaries. These are mutable data structures that contain a collection of keys and, associated with them, values
  • This structure makes them very similar to word-definition dictionaries.

 For example, the word dictionary (our key) is associated with its definition (value) in the Oxford online dictionary.

  • Dictionaries are used to quickly access certain data associated with a unique key. Uniqueness is essential, as we need to access only certain pieces of information and not confuse it with other entries. Imagine we want to read the definition of Data Science, but a dictionary redirects us to two different pages: which one is the correct one? 

Note– Technically we can create a dictionary with two or more identical keys, although due to the nature of dictionaries, it is not advisable.

  Example:

We use dictionaries when we are able to associate (in technical terms, to map) a unique key to certain data, and we want to access that data very quickly (in constant time, no matter the dictionary size). Moreover, dictionary values can be pretty complex. 

For example, our keys can be customer names, and their personal data (values) can be dictionaries with the keys like “Age,” “Hometown,” etc.

pros of the dictionary are:

  • They make code much easier to read if we need to generate key:value pairs. We can also do the same with a list of lists (where inner lists are pairs of “keys” and “values”), but this looks more complex and confusing.
  • We can look up a certain value in a dictionary very quickly. Instead, with a list, we would have to read the list before we hit the required element. This difference grows drastically if we increase the number of elements.

   cons of the dictionary are:

  • They occupy a lot of space. If we need to handle a large amount of data, this is not the most suitable data structure.

Examples:

we can create a dictionary with curly brackets ({}) or the dict() constructor:

Now let’s access an element in a dictionary. We can do this with the same method as lists:

Next, we can also modify dictionaries — for example, by adding new key:value pairs:

As we can see, a new key, “Violet”, has been added.

Note- It’s also possible to remove elements from a dictionary.

  • Sets

  • Sets in Python can be defined as mutable dynamic collections of immutable unique elements.
  •  The elements contained in a set must be immutable.
  •  Sets may seem very similar to lists, but in reality, they are very different.
  • First, they may only contain unique elements, so no duplicates are allowed.
  • Thus, sets can be used to remove duplicates from a list.

pros of the sets are:

  • We can perform unique (but similar) operations on them.
  • They are significantly faster than lists if we want to check whether a certain element is contained in a set.

     Cons of the sets are:

  • Sets are intrinsically unordered. If we care about keeping the insertion order, they are not our best choice.
  • We cannot change set elements by indexing as we can with lists.

Examples:

To create a set, we can use either curly brackets ({}) or the set() constructor. Do not confuse sets with dictionaries (which also use curly brackets), as sets do not contain key:value pairs.

 Note- that like with dictionary keys, only immutable data structures or types are allowed as set elements. This time, let’s directly create populated sets:

second example, we used an iterable (such as a list) to create a set. However, if we used lists as set elements, Python would throw an error. Why do you think it happens?

 Tip: read the definition of sets.

To practice, you can try using other data structures to create a set.

As with their math counterparts, we can perform certain operations on our sets. For example, we can create a union of sets, which basically means merging two sets together. However, if two sets have two or more identical values, the resulting set will contain only one of these values. There are two ways to create a union: either with the union() method or with the vertical bar (|) operator. Let’s make an Example:

In the above union, we can see that Tony and Joel appear only once, even though we merged two sets.

Next, we may also want to find out which names appear in both sets. This can be done with the intersection() method or the ampersand (&) operator.

Joel and Tony appear in both sets; thus, they are returned by the set intersection.

The last example of set operations is the difference between two sets. In other words, this operation will return all the elements that are present in the first set, but not in the second one. We can use either the difference() method or the minus sign (-):

What would happen if you swapped the positions of the sets? Try to predict the result before the attempt.

Finally, as a bonus, let’s compare how fast using sets is, when compared to lists, for checking the existence of an element within them.

It is obvious that using sets is considerably faster than using lists. This difference will increase for larger sets and lists.

Let’s start with the mutable data structures: tuple

  • Tuples

  • Tuples are almost identical to lists, so they contain an ordered collection of elements, except for one property: they are immutable
  • We would use tuples if we needed a data structure that, once created, cannot be modified anymore. 
  • Furthermore, tuples can be used as dictionary keys if all the elements are immutable.
  • Other than that, tuples have the same properties as lists.
  • To create a tuple, we can either use round brackets (()) or the tuple() constructor. We can easily transform lists into tuples and vice versa.

pros of the tuples are:

  • They are immutable, so once created, we can be sure that we won’t change their contents by mistake.
  • They can be used as dictionary keys if all their elements are immutable.

    Cons of the sets are:

  • We cannot use them when we have to work with modifiable objects; we have to resort to lists instead.
  • Tuples cannot be copied.
  • They occupy more memory than lists.

Examples:

Let’s take a look at some examples:

Tuples are immutable; thus, we cannot change their elements once they are created. Let’s see what happens if we try to do so:

It is a TypeError! Tuples do not support item assignments because they are immutable. To solve this problem, we can convert this tuple into a list.

However, we can access elements in a tuple by their indices, like in lists:

Tuples can also be used as dictionary keys. 

For example, we may store certain elements and their consecutive indices in a tuple and assign values to them:

If you use a tuple as a dictionary key, then the tuple must contain immutable objects:

Conclusion:

Immutable data structures, provide simplicity, thread-safety, and consistency since their state cannot be changed after creation. They are ideal for concurrent environments and situations requiring data integrity. Mutable data structures offer flexibility and efficiency for frequent updates and modifications. They are suitable for applications needing dynamic changes and in-place data manipulation. Choosing between them depends on the application’s requirements: immutables for stability and safety, and mutables for flexibility and performance. 

Learn FREE Python full Course  –

GoPython: https://www.youtube.com/watch?v=w827LUrPlu4&list=PLRMXwIyx4ALBJaVk99BbeJ3YmeG7JLP8n

GoProPYthon: https://www.youtube.com/watch?v=SuarStlBYCM&list=PLRMXwIyx4ALA55KjUHeJ2sIHBkUzO0Y6R

 

Read Also:
1. Online Coding Classes for Kids
2. Robotics for Kids
3. AI Classes for Kids

 

Facebook
Twitter
LinkedIn
Reddit