top of page
Writer's pictureBhuvaneswari Gopalan

Python Data Structures: Built-in tools to manipulate data

We all know that Python is the most popular programming language that has even made kids to start coding in a very easy and fun way. It’s easy and English-like syntax has made it a versatile language which is used in various fields all over the world.

What makes Python versatile is the built-in data structures that are very simple to code and use on any type of data and manipulate the data according to our needs.

First, let’s try to understand What Data structures are?

Data Structures are structures/containers that can store data, organize and manage them, so that it can be accessed and used efficiently. They are basically collection data types.

There are four built-in data structures in Python. They are:

  • List

  • Dictionary

  • Tuple

  • Set

The most commonly used data structures by python developers are lists and dictionaries. Now, let’s see each one of these python data structures in detail:

Data Structure #1: Lists

Python list is a collection of items of any type in a sequential order. A list can have duplicate items as every item is accessed using the index and also the items can be accessed in reverse order by using negative indexing. The lists are mutable, which means that items can be added, removed or changed even after it is created. A list can also have another list as an item in it.

Creating a list:

A list can be created by enclosing elements within [square] brackets and each item is separated by a comma. A shopping list can be an example for lists. Syntax to create a list is:

#Creating a list 
fruits = ['Apple', 'Banana', "Orange"]
print(type(fruits)) #returns type
print(fruits) #prints the elements of the list
Output:
    <class 'list'>
    ['Apple', 'Banana', 'Orange']

Accessing a list:

We can access the items in the list using indexing. Every item in the list has an index associated to it depending on the position of that item in the list. The syntax to access an item in the list:

#Access elements in the fruits list
fruits = ['Apple', 'Banana', "Orange"]
print(fruits[0]) #index 0 is the first element
print(fruits[1])
print(fruits[2])
Output:
    Apple
    Banana
    Orange

But, indexes doesn’t have to be positive always . If we want to access the list from backwards, that is in reverse order, we can use negative indexes as below:

#Access elements in the fruits list using negative indexes
fruits = ['Apple', 'Banana', "Orange"]
print(fruits[-1]) #index -1 is the last element
print(fruits[-2])
print(fruits[-3])
Output:
    Orange
    Banana
    Apple

If we have to return the elements in a range between two positions in a list, we use slicing. We have to specify start index and end index to get a range of elements from a list. The syntax is List_name[start:end:step]. Here, step is the increment value and by default it is 1.


#Accessing range of elements using slicing
fruits = ['Apple', 'Banana', "Orange"]

fruits #all elements 
['Apple', 'Guava', 'Banana', 'Kiwi'] #output

fruits[::1] #start to end with step 1
['Apple', 'Guava', 'Banana', 'Kiwi'] #output

fruits[::2] #start to end with step 2 basically index 0 & 2
['Apple', 'Banana'] #output

fruits[::3] #start to end with step 2 basically index 0 & 3
['Apple', 'Kiwi'] #output

fruits[::-1] #start to end with step 2 - reverse order
['Kiwi', 'Banana', 'Guava', 'Apple'] #output

Adding elements to a list:

We can add items to a list using the append(), extend() and insert() functions.

  • The append() function adds the element to the end of the list.

  • The insert() function adds an element to a specific position in the list which is specified along with the value

#Adding elements
fruits = ['Apple', 'Banana', "Orange"]
#Append new elements
fruits.append('Kiwi')
print(fruits)

Output: 
    ['Apple', 'Banana', 'Orange', 'Kiwi']
#Insert elements in to the list
fruits.insert(1,'Guava') #inserts Guava as second element is the list since the index is specified as 1
print(fruits)

Output: 
    ['Apple', 'Guava', 'Banana', 'Orange', 'Kiwi']

Deleting elements from a list:

Similar to adding elements, removing/deleting elements from a list is also very easy and it is achieved using del(), remove() and pop() methods. To clear the entire list clear() function can be used.

  • del() function deletes element at the given index

  • pop() function removes the element from the list at a given index and also you can assign the deleted element to a variable. If the index value is not specified, then it removes the last element in the list.

  • remove() function removes an element by its value.

  • clear() function empties the list.

#Deleting elements from the list
fruits = ['Apple', 'Guava', 'Banana', 'Orange', 'Kiwi']

#del() function
del fruits[3] #delete element at index 4
print(fruits)

Output: 
    ['Apple', 'Guava', 'Banana', 'Kiwi']
    
#pop() function
del_fruit = fruits.pop(2)
print(del_fruit)
print(fruits)

Output: 
    'Banana'  
    ['Apple', 'Guava', 'Orange', 'Kiwi']

#Remove function
fruits.remove('Apple')
print(fruits)

Output : 
    ['Guava', 'Banana', 'Orange', 'Kiwi']
    
#Clear() function
fruits.clear()
print(fruits)

Output : 
    [] # clears the list

Other Functions:

We have several other functions that can be used when working with lists. They are:

  • The len() function returns the length of the list.

  • The index() function finds the index value of value passed where it has been encountered the first time.

  • The count() function finds the count of the value passed to it.

  • The sorted() and sort() functions are to sort the values of the list. The sorted() has a return type whereas the sort() modifies the original list.

#Other functions for list
num_list = [1, 2, 3, 10, 20, 10]
print(len(num_list)) #find length of list
print(num_list.index(10)) #find index of element that occurs first
print(num_list.count(10)) #find count of the element
print(sorted(num_list)) #print sorted list but not change original
num_list.sort(reverse=True) #sort original list
print(num_list)
Output:
6
3
2
[1, 2, 3, 10, 10, 20]
[20, 10, 10, 3, 2, 1]

Data Structure #2 : Dictionary

Dictionary is another data structure of python which is unordered, meaning the elements are not stored in the same order as they are inserted. This is because the elements in a dictionary are not accessed by the index value.

In a dictionary, data is stored in the form of key value pairs. The elements value is accessed using the key.

Creating a Dictionary:

Dictionaries are created by writing the key and values using a { curly } bracket separated by a colon or using the dict() function.

#Creating Dictionaries
new_dict = {} #empty dictionary
print(new_dict)

new_dict = {1: 'Python', 2: 'Java'} #dictionary with elements
print(new_dict)
Output:
    {}
    {1: 'Python', 2: 'Java'}

Changing and Adding key, value pairs:

To change the values of the dictionary, we will use the keys to access the key and then change the value accordingly. To add values, we just add another key-value pair as shown below.

#Changing and Adding key, value pairs
lang_dict = {'First': 'Python', 'Second': 'Java'}
print(lang_dict)

lang_dict['Second'] = 'C++' #changing element
print(lang_dict)

lang_dict['Third'] = 'Ruby' #adding key-value pair
print(lang_dict)
Output:
    {'First': 'Python', 'Second': 'Java'}
    {'First': 'Python', 'Second': 'C++'}
    {'First': 'Python', 'Second': 'C++', 'Third': 'Ruby'}

Accessing Elements in a Dictionary:

The elements in the dictionary can be accessed using the keys only. We can use either the get() function or just pass the key values to retrieve the values.

#Accessing Elements
lang_dict = {'First': 'Python', 'Second': 'Java'}
print(lang_dict['First']) #access elements using keys
print(lang_dict.get('Second'))
Output:
    Python
    Java

Deleting key, value pairs in a dictionary:


These are the functions that are used in Dictionary to delete elements.

  • pop() - To delete the values which returns the value that has been deleted.

  • popitem() - To retrieve the key-value pair which returns a tuple of the key and value.

  • clear() - To clear the entire dictionary

#Deleting key, value pairs in a dictionary
lang_dict = {'First': 'Python', 'Second': 'Java', 'Third': 'Ruby'}
a = lang_dict.pop('Third') #pop element
print('Value:', a)
print('Dictionary:', lang_dict)

b = lang_dict.popitem() #pop the key-value pair
print('Key, value pair:', b)
print('Dictionary', lang_dict)

lang_dict.clear() #empty dictionary
print(lang_dict)
Output:
    Value: Ruby #pop element
    Dictionary: {'First': 'Python', 'Second': 'Java'}
    Key, value pair: ('Second', 'Java') #pop the key-value pair
    Dictionary {'First': 'Python'}
    {} #empty dictionary

Other Functions:

These are some of the other functions that can be used with dictionaries to get the keys, values and key-value pairs etc.

#Other functions for dictionary
lang_dict = {'First': 'Python', 'Second': 'Java', 'Third': 'Ruby'}
print(lang_dict.keys()) #get keys
print(lang_dict.values()) #get values
print(lang_dict.items()) #get key-value pairs
print(lang_dict.get('First'))
Output:
    dict_keys(['First', 'Second', 'Third'])
    dict_values(['Python', 'Java', 'Ruby'])
    dict_items([('First', 'Python'), ('Second', 'Java'), ('Third', 'Ruby')])
    Python
    
Data Structure #3 : Tuple

Tuples are the same as lists with the exception that the data once entered into the tuple cannot be changed no matter what, which means that tuples are immutable. So, once a tuple is generated, no value can be added, deleted, or edited.

Creating a Tuple:

We create a tuple using ( parenthesis ) or using the tuple() function.

#Creating Tuples
my_tuple = (1, 2, 3) #create tuple
print(my_tuple)
Output:
    (1, 2, 3)

Accessing Elements in a Tuple:

Accessing the tuple elements is similar to that of a list.

#access elements
my_tuple2 = (1, 2, 3,'new') 
for x in my_tuple2: 
    print(x) # prints all the elements in my_tuple2
print(my_tuple2)
print(my_tuple2[0]) #1st element
print(my_tuple2[:]) #all elements
print(my_tuple2[3][1]) #this returns the 2nd character of the element at index 3 
print(my_tuple2[-1]) #last element
Output:
    1
    2
    3
    new
    (1, 2, 3, 'new')
    1
    (1, 2, 3, 'new')
    e
    new


Appending Elements from another tuple:

To append the values, we can use the ‘+’ operator.

#Appending elements
my_tuple = (1, 2, 3)
my_tuple = my_tuple + (4, 5, 6) #add elements
print(my_tuple)
Output:
    (1, 2, 3, 4, 5, 6)

Tuple assignment:

Tuple packing and unpacking are some useful operations that we can perform to assign values to a tuple of elements from another tuple in a single line.

Tuple packing is nothing but when we create a tuple by adding individual values. Tuple unpacking is just the opposite-assigning values to variables from a tuple.

#tuple packing
planets = ('Earth', 'Mars','Jupiter')

#tuple unpacking
a,b,c = planets
print(a)
print(b)
print(c)
Output:
    Earth
    Mars
    Jupiter    

Data Structure #4 : Sets

Sets are a collection of unordered elements that are unique. This means that even if the data is repeated more than one time, it would be entered into the set only once.

Creating a set:

Sets are created using the {curly} braces and values are passed to it.

#Creating sets
new_set = {1, 2, 3, 4, 4, 4, 5} #create set
print(new_set)
Output:
    {1, 2, 3, 4, 5}

Adding elements to a set:

To add elements, we use the add() function and pass the value to it.

#Adding elements to a Set
new_set = {1, 2, 3}
new_set.add(4) #add element to set
print(new_set)
Output:
    {1, 2, 3, 4}

Operations on sets:

The different operations that can be performed on a set are shown below.

  • The union() function combines the data present in both sets.

  • The intersection() function finds the data present in both sets only.

  • The difference() function deletes the data present in both and outputs data present only in the set passed.

  • The symmetric_difference() does the same as the difference() function but outputs the data which is remaining in both sets.

  • clear() function empties the set.

#Operations on set
my_set = {1, 2, 3, 4}
my_set_2 = {3, 4, 5, 6}

print(my_set.union(my_set_2))
print(my_set.intersection(my_set_2))
print(my_set.difference(my_set_2))
print(my_set.symmetric_difference(my_set_2))

my_set.clear()
print(my_set)
Output:
    {1, 2, 3, 4, 5, 6}
    {3, 4}
    {1, 2}
    {1, 2, 5, 6}
    set()

That wraps up all the built-in data structures that are available in Python. Isn’t python a wonderful language? The language provides us with so many options to efficiently manage, organize and access data and learning about these basic built-in data structures of python is the key aspect of the python learning journey.

Hope, this blog provided a good introduction to the built-in data structures in python and hope this will be useful to everyone who is trying to learn python.

Thanks for reading!





663 views

Recent Posts

See All
bottom of page