Mastering Python Data Structure: 3 Common Data Structures that You Need to Know

Python is a versatile and likable programming language used by various data professionals. Pythons' prevalence in the data science community, particularly in data analysis is because of its underlying built-in data structure that allows for magnificent data storage and manipulation.

In our last article, we learned how variables act as containers to store values, whose names are used to refer to the values later on in the program.

Python has fundamentally three common data structures: dictionaries, tuples and lists. These data structures somewhat extend the functionalities of variables in the way they store, organise and manipulate data. Here is an overview of the three data structures in Python.

A Dictionary

A dictionary deals with key-value pairs where the elements in the data dictionary are not in any particular order. It is essential to note the 'words' ‘key-value pairs’. Because the keys and values are stored together as pairs. The keys as a matter of reference, act as names that identify the values associated with them. So that every key has a corresponding value, and values are retrieved by their keys.

While the values associated with keys can be any data type, i.e. string, numbers etc, each key is unique. Dictionaries are flexible, meaning that their elements can be modified after creation. Dictionaries are created with curly braces {}, and keys are separated from values with colons: See a working example below:

employee ={"name":"Sam", "profession": "Data analyst", "experience":5}

print(employee["name"])

output: Sam


# Adjust years of experience

employee["experience"]=10

print(employee["Experience"])

output: 10


In the example above, the dictionary employee holds three key-value pairs. The keys are "name", "profession", and "experience",. Their corresponding values are "Sam", “Data analyst”, and 5 respectively. Next, we change the years of experience to 10 and the terminal outputs the result value of 10.

Characteristics of the Data Dictionaryb>

Unordered: Items in a dictionary are not sequential. This means that they are not stored in any specific order. Subsequently, they cannot be accessed by their relative positions, rather they are accessed by their keys. Dynamic size: Whether the size of a dictionary increases or decreases is predicated on adding or removing key-value pairs.

Mutability: This means that one can modify items in a dictionary after creation by manipulating the key-pair values.

Uniqueness of Keys: Whereas the values of a key can be any data type, the associated keys must be absolutely unique.

Quick Key-value access: Using their corresponding keys, values can quickly and easily be retrieved. This makes a data dictionary an efficient approach to manipulating data.


A List

A list is an organized collection of elements, where multiple items are stored in a single variable in sequence, and where every element in the collection is assigned an index. This index is unique and begins from zero.

Elements in a list are related and can have any data type. The indexing of elements means that items in a list can have duplicate values. A list is mutable, meaning that the items in the list can be changed or modified after it has been created. Lists are created using square brackets []. See an example of a list:

party = ["conservative", "labour", "liberal democrat", "green party"]


#modifying the above list

party.remove("labour")

print(party)

output: ['conservative', 'liberal democrat', 'green party']


Characteristics of the list Ordered: Because the elements of a list are ordered, they are fixed and can be accessed by the positional index.

Dynamic: The dynamic nature of the list means that they can do as the occasion demands. Meaning that items can be added or removed when required. The beautiful thing is that the list would adjust in response to any changes.

Mutable: A list is mutable so that the element of the list can be altered.

Heterogeneous: The heterogeneous nature of a list means that it can contain different data types in Python programming.

Duplicates: Because elements of a list are accessed by an index, they can contain duplicate values.


Nested: A list can contain other lists, making a list a complex data structure.

A Tuple A tuple is an ordered data structure but unlike a list it is immutable and it is defined with parenthesis (). Like a list data structure, they can contain different data types and they can be accessed by index.

It also means that the elements of a tuple cannot be modified. It is lightweight and therefore low on memory usage. Interestingly, because tuples are immutable, they can be accessed by keys. Typical use cases are that tuples are used to store constants like days of the weeks or months. Here is a working example of a tuple:

Storing constants

DAYS_OF_WEEK = ('Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday')

MONTHS_OF_YEAR = ('January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December')


Grouping related data

location = (37.7749, -122.4194)


Using dictionary keys

person = ('John', 'Doe', 30)

people = {person: 'San Francisco'}


Characteristics of a Tuple

Because a tuple is both immutable and ordered at the same time, it is characteristics are a combination of a data dictionary and that of a list. Which means that it is:

  • Ordered
  • Immutable
  • Can contain duplicates
  • Heterogenous
  • Indexable
  • Can be used as a dictionary
  • Low on memory

  • Summary

    In conclusion, data structures are the foundation on which programs are built and Python has a ton of them. In this article, we discussed three popular Python data structures: tuple, data dictionary and list. Each has its benefits and drawbacks. For instance, some data structures are mutable, others are not. Some can be accessed by keys while others are accessed by index numbers. It is abundantly clear from the article that the application of a specific data structure is predicated on the particular use case.