What Is a Data Structure in Python?


Scott Campbell

A data structure is a way of organizing and storing data in a computer so that it can be used efficiently. In Python, there are several built-in data structures that help us manage and manipulate data effectively. Let’s take a closer look at some commonly used data structures in Python.


A list is one of the most versatile data structures in Python. It is an ordered collection of elements, enclosed in square brackets ([]), and separated by commas. Lists can contain elements of different types, such as numbers, strings, or even other lists.

To create a list, you simply assign values to a variable using the square bracket syntax:

my_list = [1, 2, 3, "hello", True]

We can access individual elements of a list using their index. The index starts from 0 for the first element and increments by 1 for each subsequent element:

print(my_list[0])  # Output: 1
print(my_list[3])  # Output: hello

Lists are mutable, which means we can modify their elements after they have been created. We can add new elements to a list using the append() method:

print(my_list)  # Output: [1, 2, 3, "hello", True, 4]


A tuple is similar to a list but with one key difference – it is immutable. This means that once we create a tuple, we cannot modify its elements. Tuples are defined using parentheses (()) instead of square brackets ([]).

my_tuple = (1, 2, 3)

Since tuples are immutable, they are useful when we want to store a collection of values that should not be changed. Tuples also offer some performance benefits over lists.


A dictionary is a data structure that stores key-value pairs. It is an unordered collection of elements enclosed in curly braces ({}). Each element in a dictionary consists of a key and its associated value, separated by a colon (:).

my_dict = {"name": "John", "age": 25}

Dictionaries provide fast access to values based on their keys. We can access the value associated with a key using the square bracket syntax:

print(my_dict["name"])  # Output: John

We can also modify or add new key-value pairs to a dictionary:

my_dict["name"] = "Jane"
my_dict["city"] = "New York"
print(my_dict)  # Output: {"name": "Jane", "age": 25, "city": "New York"}


A set is an unordered collection of unique elements. It is defined using curly braces ({}) or the set() function. Sets do not allow duplicate values.

my_set = {1, 2, 3}

Sets support various mathematical operations like union, intersection, and difference. They are useful when we need to perform operations on distinct elements.


Understanding data structures is essential for efficient programming in Python. By utilizing the built-in data structures such as lists, tuples, dictionaries, and sets, you can organize and manipulate your data effectively. Each data structure has its own characteristics and use cases, so choose the one that best suits your needs.

With these powerful data structures at your disposal, you are well-equipped to handle complex data in Python!

Discord Server - Web Server - Private Server - DNS Server - Object-Oriented Programming - Scripting - Data Types - Data Structures

Privacy Policy