Python Data Structures

Last Updated : 15 Apr 2026

In Python, data structures are used to organize, store, and manage data so it can be accessed efficiently. Python is a high-level, object-oriented language that makes it easy to learn and work with data structures and their algorithms.

Python provides several built-in data structures, such as list, tuple, set, and dictionary. In this chapter, we will learn about these data structures with examples.

Python List

Python Lists are used to store multiple items of different data types in a single variable. It works in the same manner as arrays, but with only one difference: in lists, not all the items need to be of the same data type.

Lists are mutable (can be modified, deleted or replaced) and can contain duplicate items. A list works in the same way as an array in C, vectors in C++ or ArrayList in Java.

Example

In the following example, we are creating a simple list and a nested list and printing their elements.

Execute Now

Output:

List 1: ['Tpoint', 12, False, 1.6, 3000000000000.0]
List 2: [['Tpoint', 'Tech'], 3, True, 'Hello', 1.69]

Explanation:

In the above example, we have created a simple list using the square brackets '[]'. This list consists of different elements of data types. Similarly, we have created a nested list (a list within a list).

Python Tuple

Python Tuple is defined as a collection of objects. It works in a similar way like list with an only difference is that Tuples are immutable (which means once you create a tuple in Python, we cannot add, delete or modify and element in it). Just like a List, a Tuple can also contain elements of various types.

In Python, tuples are created by placing a sequence of values separated by 'comma' with or without the use of parentheses for grouping of the data sequence.

Example

In the following example, we are creating a simple tuple and a nested tuple and printing their elements.

Execute Now

Output:

Tuple 1: ('Tpoint', 19, True, 1.6, 5000.0)
Tuple 2: (('Hello', 'World'), 13, False, 'Tpoint', 6.9)

Explanation:

In the above example, we have created a simple tuple using the parentheses '()'. This tuple consists of different elements of data types. Similarly, we have created a nested tuple (a tuple within a tuple).

Python Set

Python Set is a collection of unordered data used to store multiple elements in a single variable. Set is also mutable, which means once they create a set in Python, you cannot change or modify its elements.

In Python sets, duplicity is not allowed; therefore, you cannot have two items with the same value, which helps eliminate duplicate entries. It is commonly used to include membership testing. Set uses the popular Hashing technique that permits operations like insertion, deletion, and traversal in O(1) time on average.

Example

In the following example, we are creating sets and printing their elements, where duplicate values are automatically removed.

Execute Now

Output:

Set 1: {False, 19.2, 'tpoint', 21, 'hello', 40}
Set 2: {True, ('tpoint', 'tech'), 'welcome', 10, 13.6}

Explanation:

In the above example, we have created a set by enclosing some items with curly braces '{}'. Here, we can observe that the duplicate elements are eliminated from the set. Moreover, we can observed that the elements in the set are not ordered. We have created another set containing a tuple, an immutable sequence.

Python Dictionary

Python Dictionary is used to store data values key: value pair. It's different from lists as it is ordered and changeable, and it doesn't contain any duplicate elements. The values provided can be of any data type and may even contain duplicate data; however, the keys are immutable and should not contain duplicate values.

In the Dictionary, indexing is done using keys. They internally use the concept of hashing and are of any hashable type (which means an object that can never change, like strings, numbers, tuples, etc.)

Example

In the following example, we are creating a dictionary and a nested dictionary, and printing their elements, where duplicate keys are overwritten.

Execute Now

Output:

Dictionary 1: {'name': 'Johnson', 'age': 24, 'profession': 'Software Developer', 'company': 'Tpoint Tech'}
Dictionary 2: {'name': 'Sachin', 'dob': {'date': 11, 'month': 'March', 'year': 1999}, 'profession': 'Web Developer', 'company': 'Tpoint Tech'}

Explanation:

In the above example, we have created a dictionary by enclosing some 'key: value' pairs with curly braces '{}'. Here, we can see key 'age' is repeated, therefore the value of the first key is updated with the second value of the key as dictionary only contains unique keys. We have also created a nested dictionary (a dictionary as a value of another dictionary).

Python Strings

Python Stringsare arrays of bytes representing Unicode characters. For example, "tpointtech" is a string containing the sequence of characters - 't', 'p', 'o', 'i', 'n', 't', 't', 'e', 'c', 'h'.

Example

In the following example, we are creating a string and performing operations like finding length and accessing its elements.

Execute Now

Output:

My String: Tpoint Tech

Given String: Welcome to Tpoint Tech
Length of String: 22
First Element of String: W
Last Element of String: h
Fourth Element of String: c

Explanation:

Here, we have created a string by enclosing a text with double quotation marks ("…"). We have also performed some operations on string, such as finding its length and also printing the different elements of the string.

Collections Module

Python Collection Module acts as a container that provides various in-built datatypes. Let's understand them one by one.

S. No.Data StructuresDescription
1CountersThe Python counter is a sub-class of the dictionary. It is used to keep the count of the elements in an iterable in the form of an unordered dictionary where the key represents the element in the iterable and value represents the count of that element in the iterable.
2OrderedDictPython OrderedDict is like a dictionary, with the only difference being that it to maintain the insertion order of keys in a dictionary.
3DequePython Deque stands for Doubly Ended Queue. It is a list-like container used for accessing fast append and pop operations from both sides of the container. It supports both FIFO (First In, First Out) and LIFO (Last In, First Out) operations.
4ChainMapPython ChainMap encapsulates different dictionaries into a single variable. It finds the key by searching all the dictionaries one by one unless the required value is found.
5UserDictPython UserDict wraps all the dictionary objects. It acts as a container used by Python developer when they want to create their dictionary with some modified or new functionality.