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[].

inputList = [“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.

1) Getting the Size of the List Using the len() function

Get the length/size of the list using the len() function (The number of items in an object is returned by the len() method. The len() function returns the number of items in a list when the object is a list) and create a variable to store it.

Example

# input list lst = ["Hello", "TutorialsPoint", 78, "Hi", "Everyone"] # getting list length listLength = len(lst) # Printing the size of a list print("Size of a List = ", listLength)

Output

('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.

Example

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

Output

('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 inputList = [10, 20, 30, 40, 50] print("Input list elements:") # traversing through all elements of the list using for loop for element in inputList: # printing each element of the list print(element)

Output

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.

Example

The following program repeats the list the given number of times using the * operator −

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

Output

[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. The slicing operator can be represented by the syntax

[start:stop:step]

Example

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

Output

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.

Example

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

Output

('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

Getting a list of all the keys from a dictionary using dict.keys() method

Print the list of all the keys of a dictionary with the keys() function by applying it to the input dictionary and convert the result to a list using the list() function (converts the sequence/iterable to a list).

Example

# input dictionary
demoDictionary = {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(demoDictionary.keys()))

Output

[10, 12, 14]

Conclusion

In this article, we learned about compound data types and data structures, as well as some of their examples.

Updated on: 23-Aug-2023

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements