What are compound data types and data structures in Python?

In this article, we will explain what are the compound datatypes and data structures in Python.

Variables have so far only stored one value. What if we wish to save many related values? We could simply create distinct variables for each. But what if we don't know how many values will be present? What if we wish to use these values within a loop?

Compound data structures are data types that can store a large number of values. In Python, there are various types of compound data structures.

  • We will mostly concentrate on Lists.

  • In the end, we will take a quick look at Sets, Tuples, and Dictionaries.

Lists

In Python, a list is an ordered sequence that can hold several object types such as integer, character, or float. In other programming languages, a list is equivalent to an array.

A list is just a list of values separated by commas enclosed in square brackets [].

# Creating a list with mixed data types
input_list = ["hello", "tutorialspoint", 1, 3.5, "python"]
print(input_list)
['hello', 'tutorialspoint', 1, 3.5, 'python']

Operations on List

There are numerous operations that may be performed on lists in order to create expressions from them.

Getting the Size of the List Using len()

Get the length/size of the list using the len() function. The len() method returns the number of items in a list ?

# input list
lst = ["Hello", "TutorialsPoint", 78, "Hi", "Everyone"]

# getting list length
list_length = len(lst)

# Printing the size of a list
print("Size of a List =", list_length)
Size of a List = 5

Accessing List Elements Using Indexing

The term "indexing" refers to an element of an iterable based on its position inside the iterable. The indexing begins from 0. The first element in the sequence is represented by index 0.

Negative indexing begins from -1. The last element in the sequence is represented by index -1.

# input list
input_list = [1, 4, 8, 6, 2]

# accessing the list element at index 2 using positive indexing
print("Element at index 2:", input_list[2])

# accessing the last element in list using negative indexing
print("Last element of an input list:", input_list[-1])
Element at index 2: 8
Last element of an input list: 2

NOTE: When we attempt to use an index that does not exist or is too large, it throws an IndexError.

Iterating on Lists

Using For Loop

The following program prints all the list elements using the for loop ?

# input list
input_list = [10, 20, 30, 40, 50]

print("Input list elements:")
# traversing through all elements of the list using for loop
for element in input_list:
    # printing each element of the list
    print(element)
Input list elements:
10
20
30
40
50

Repetition Operator (*) on List Items

Python List also includes the * operator, which allows you to create a new list with the elements repeated the specified number of times ?

# input list
input_list = [5, 6, 7]

# Repeating the input list 2 times using the * operator
print(input_list * 2)
[5, 6, 7, 5, 6, 7]

Here, we took a list of random values and multiplied it twice with the * operator, so that the output consists of the given list repeated twice.

Tuples in Python

A Tuple is an immutable sequence data type that can contain elements of various data types. A tuple is just a collection of Python objects separated by commas. Tuples are faster than lists since they are static in nature.

The syntax of the list and tuple is a bit different. Lists are denoted by square brackets [], and Tuples by parenthesis ().

Tuple Slicing

We can use tuple slicing. It is similar to how we use strings and lists. Tuple slicing is used to obtain a variety of items. We also use the slicing operator to perform tuple slicing with the syntax [start:stop:step].

# Input tuple
given_tuple = ("Welcome", "this", "is", "TutorialsPoint", "Website", 10)

# Slicing with start and stop values(indices)
print('Tuple slicing from index 1 to index 6:', given_tuple[1:6])

# Slicing with only stop values(indices)
print("Tuple slicing till index 7:", given_tuple[:7])
Tuple slicing from index 1 to index 6: ('this', 'is', 'TutorialsPoint', 'Website', 10)
Tuple slicing till index 7: ('Welcome', 'this', 'is', 'TutorialsPoint', 'Website', 10)

Accessing Tuple Elements Using Indexing

As the List, the tuple also uses indexing to access its elements. The only difference is that a tuple is immutable (cannot be changed) whereas a list is mutable ?

# input tuple
input_tuple = (1, 4, 8, 6, 2)

# accessing the tuple element at index 2 using positive indexing
print("Element at index 2:", input_tuple[2])

# accessing the last element in tuple using negative indexing
print("Last element of an input tuple:", input_tuple[-1])
Element at index 2: 8
Last element of an input tuple: 2

NOTE: When we attempt to use an index that does not exist or is too large, it throws an IndexError.

Dictionaries in Python

A dictionary is a collection of key-value pairs. You can extract keys from a dictionary using the keys() function. Convert the result to a list using the list() function ?

# input dictionary
demo_dictionary = {10: 'TutorialsPoint', 12: 'Python', 14: 'Codes'}

# Printing the list of keys of a dictionary using the keys() function
# list() methods convert an iterable into a list
print(list(demo_dictionary.keys()))
[10, 12, 14]

Conclusion

Compound data structures in Python include lists, tuples, dictionaries, and sets. Lists are mutable and ordered, tuples are immutable and ordered, while dictionaries store key-value pairs for efficient lookups.

Updated on: 2026-03-26T21:56:08+05:30

5K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements