Real-Time use of Python Data Structures and its benefits

Hello readers, welcome back to know the Python Data Structures and its importance in a real-time situation. Python is one of the most widely used languages for the development of a wide range of applications from small to large scale. This article supports how and when we use python data structures in projects to simplify data challenges and conclude the practical tour of all the data structures.

List of existing Python Data Structures:

  • Lists
  • Tuples
  • Sets
  • Strings
  • Dictionaries

python data structures


The lists or arrays in python are very flexible data structures in which we can mix values of several types, or that are of a single type. Your declaration is simple and obeys a standard JSON format.
Let us discuss how to create a basic list and list items.

>>> [1, 2, 3, 4]
[1, 2, 3, 4]
>>> [''Srikanth', 'Bashaboina']
[Srikanth', 'Bashaboina']
>>> [0, 1.5, "Srikanth"]
[0, 1.5, "Srikanth"]

In the above List, it accepts different types of items as ‘int’, ‘float’, and ‘str’.
Also, a list in python can contain in one or more of its positions another list of ‘n’ dimensions. thus forming an orthogonal list.

>>> list1 = [1, 2]
>>> list2 = [1.5, 2, list1]
>>> list2
[1.5, 2, [1, 2]]

List Append and Extend:

The two basic operations in the lists are ‘append’ and ‘extend’. The difference between these two operations is that append adds an element to the end of the list, while ‘extend’ is able to add another list to the end of the list. Let’s see some examples:

>>> list1 = ['a','b']
>>> list1.append('c')
>>> list1
['a', 'b', 'c']

>>>list2 = ['a', 'b', 'c']
>>> list2.extend(['d', 'e','f'])
>>> list2
['a', 'b', 'c', 'd', 'e', 'f']

Operations with lists

Now let’s see all the possible basic operations with lists in python:

  • index: It returns the position in the list of the indicated element
  • insert: Insert an item in the list according to the desired index
  • remove: Similarly, an item can be removed from the list
  • pop: Pop returns the last item in the list and then removes it from it
  • count: Returns the items account currently in the list
  • sort: There are several possibilities to order an array as indicated by the sort method. To sort in ascending order by default
>>> list1 = ['a','b','c','b', 'a']
>>> list1.index('b') # 'index' Operation
>>> list2 = ['b', 'c', 'b']
>>> list2.insert(2, 'a') # 'insert' Operation at desired location
>>> list2
['b', 'c', 'a', 'b']

>>> list2.remove(2, 'a') # 'remove' Opeartion
>>> list2
['b', 'c', 'b']
>>> list2.pop() # 'pop' Operation
>>> list2
['b', 'c']

list2.count('b') # 'count' Operation
# Sorting 
>>> list3 = ['a','b','c','b']
>>> list3.sort()
>>> list3
['a', 'b', 'b', 'c']

>>> list3 .sort(reverse=True)
>>> list3 
['c', 'b', 'b', 'a']

>>> my_list = ['a', 'c' ,'b']
>>> my_list.reverse()
>>> my_list
['b', 'c', 'a']

# using del statement
>>> my_list_new = [-1, 1, 66.25, 333, 333, 1234.5]
>>> del my_list_new[0]
>>> my_list_new 
[1, 66.25, 333, 333, 1234.5]
>>> del my_list_new [2:4]
>>> my_list_new 
[1, 66.25, 1234.5]
>>> del my_list_new [:]
>>> my_list_new 


Tuples are like lists, except that in this case, they are immutable. A tuple consists of several values separated by commas. Some list functions also work in tuples, such as  ‘len()’ and syntax to get portions of the array or slicing.  


An important thing to note with tuples is that since parentheses are also used in python to group expressions if we wanted to create a tuple with a single value we simply add a comma at the end like shown in the example.

>> my_tuple = (1, 2, 3) # declaration of the Tuple
>>> my_tuple[0]
>>> len(my_tuple) # len of the tuple
>>> my_tuple[1:]
2, 3

my_tuple2 = (1)
>> my_tuple2 

>> my_tuple2 = (1,) # create a tuple with a single value we simply add a comma at the end
>>> my_tuple2 
>>> my_tuple2 [0]


Sets in python are lists without a specific order but whose elements are unique, that is, there is no repetition and duplicate. And to define one we must explicitly indicate that we want the list to be a set with the function with the same name.

>>> # Demonstrate set operations on unique letters from two words
>>> set1 = set('abcdefgrrabhf')
>>> set2 = set('piggypackabc')
>>> set1   # unique letters in a
{'r', 'h', 'e', 'b', 'd', 'a', 'c', 'g', 'f'}                          
>>> set1 - set2    # letters in a but not in b
{'r', 'd', 'h', 'e', 'f'}                             
>>> set1 | set2     # letters in a or b or both
{'r', 'h', 'y', 'k', 'e', 'b', 'i', 'd', 'a', 'c', 'p', 'g', 'f'}                             
>>> set1 & set2    # letters in both a and b
{'g', 'a', 'c', 'b'}                            
>>> set1 ^ set2     # letters in a or b but not both
{'y', 'r', 'h', 'k', 'e', 'i', 'd', 'p', 'f'}

Real-Time Usage of the above Python Data Structures:

Implementing data structures mainly depends on how you save the data types for the data validation.
The list data type is used as arrays, data validation and validating user registration data with regular expression validation like name, email, id, contact details.
Tuples are used to remove the duplicates from the original set of values.

The next types of Data Structures are continued in the Next Article Strings and Dictionaries
Click here to get to know Python Operator Precedence Cheat Sheet