Commonly Used Python Functions For Data Structure

This is the second part of the article following the previously published, Commonly Used Python Functions. In this article, we’ll discuss the Python functions used for Python List, Python Dictionaries, Python Tuple, and Python Set. These built-in functions come in very handy in day-to-day usage for various purposes from using it in the development of applications, data science projects, web scraping, and more.   

Data Structure

Data Structure in Computer Science is a process of organizing and managing data with a storage format such that efficient access and modification can be attained during its usage. Data Structure and Algorithms are part of the interviews in competitive jobs markets for Silicon Valley companies for the proper understanding of Data Structure is elemental for bigger complex problems to solve. Let us discuss some of the data types supported by Python.  

List 

Lists allow the storing of multiple items in a single variable. It is one of the four built-in data types available in Python.  

Creating a List 

A list in Python can be created by using square brackets to place elements separated by commas.  

#list of fruits 
fruit = ['apple', 'mango', 'litchi', 'strawberry'] 

Commonly Used Python Functions For Data Structure

#list of numbers 
num_list = [9,8,7,6,5] 

Commonly Used Python Functions For Data Structure

#list of mixed data types 
mixed_list = [3,8,"C# Corner", 'x', 2] 

Commonly Used Python Functions For Data Structure

Accessing Elements of the List 

The elements of the list can be accessed in numerous ways, and the List Index is one of the common ones.  

The indexing of the list starts with 0. Hence, using (nth element – 1) inside the square bracket denotes the required nth element.  

mixed_list[4] 

Commonly Used Python Functions For Data Structure

This resulted with 2 as an output for it is the 5th element in the list as the indexing starts from 0.  

append() 

The append() function enables the addition of elements to the end of the list. Let us say, we want to add more elements to the fruit list, we can do it simply by using the append method.  

fruit.append('papaya') 

Commonly Used Python Functions For Data Structure

remove() 

The remove method can be used to remove the element by specifying it. This will remove the first item we specified in case of multiple similar elements.  

fruit.remove('litchi')

Commonly Used Python Functions For Data Structure

count() 

The count method returns the total number of the specified items in the list.  

fruit.count('mango') 

Commonly Used Python Functions For Data Structure

This outputted 1 as there is only 1 such element in the list.  

clear() 

The clear method can be used to remove all the elements present in the list. It enacts the deletion of data leaving the list empty.  

fruit.clear() 

Commonly Used Python Functions For Data Structure

We can see, when we call the list fruit later, the list is totally empty.  

Dictionaries  

Dictionaries in Python can be understood as the collection of key-value pair implementation of data structure. The key-value pair in the dictionary maps key to its associated value. It is also known as an associate array. Dictionaries are unordered collections of data values, unlike List which are indexed.  

fruit_dict = {
    'name': 'Apple',
    'scientific_name': 'Malus domestica',
    'origin': 'Central Asia',
    'variation': 7500,
    'flavor_profile': ['Sweet', 'Sour', 'Bitter']
}

Commonly Used Python Functions For Data Structure

keys()

The keys() method returns the key part of the dictionary.  

fruit_dict.keys()

Commonly Used Python Functions For Data Structure

Here, name, scientific_name, origin, variation, and flavor_profile are the keys in the Dictionary fruit_dict.  

values() 

The values() methods return the values part of the dictionary. 

fruit_dict.values()

Commonly Used Python Functions For Data Structure

Here, 'Apple', 'Malus Domestica, 'Central Asia', 7500, ['Sweet', 'Sour', 'Bitter'] and the values which are the combination of String, Numeric Value and even another data type such as a list.  

clear() 

As discussed above, the clear() method can be used with Dictionaries too which will remove all the key-value pairs from the Dictionary leaving an empty dictionary.  

fruit_dict.clear() 

Commonly Used Python Functions For Data Structure

Tuple  

A tuple is similar to List with the main difference being its immutable nature ie. The elements of a tuple cannot be changed as of the List. In contrary to List, Tuple is created using a Small Bracket ().  

Let us create a tuple of fruits.  

fruit_tuple = ('apple', 'mango', 'litchi', 'strawberry') 

Commonly Used Python Functions For Data Structure

If we try to use methods like clear that we tried for list and dictionaries,  

fruit_tuple.clear()  

Commonly Used Python Functions For Data Structure

We obtain an attribute error. This is due to the immutable characteristics of Tuple.  

del()

Howsoever, though we can’t delete a single particular part of the tuple, we can delete the entire tuple using keyword del.  

del(fruit_tuple) 

Now, if we check the fruit_tuple, we can see the error of the tuple not being defined as it is deleted entirely.  

Commonly Used Python Functions For Data Structure

We can also, check out the individual elements using Indexing similar to List as Tuple is ordered.  

Commonly Used Python Functions For Data Structure

fruit_tuple[0] 

 

fruit_tuple[2] 

The indexing from 0 for the first element is similar to as of the List and thus, (nth – 1) elements will be showcased.  

Set 

Sets are the unordered collection of different data and the Set of Python depicts that essence. It is mutable similar to List. Moreover, a mathematical set operations such as intersection, the union can be performed with Sets in Python. Sets are created using Curly Brackets {} just like in mathematics.  

fruit_set = {'apple', 'mango', 'litchi', 'strawberry', 'avocado', 'grape'} 

Commonly Used Python Functions For Data Structure

Duplication of elements cannot be done in Set. If we repeat a similar element, the set will only consider 1 count of such element. Hence, unlike a list that can have more than 1 count for each element, Sets will always have one count such that, sets don’t contain the attribute count as it will not be in usage.  

fruit_set = {'apple', 'mango', 'litchi', 'strawberry', 'avocado', 'grape', 'grape'} 

Commonly Used Python Functions For Data Structure

Commonly Used Python Functions For Data Structure

Operations in Set 

The mathematical set operations such as union, intersection, the difference can be performed in the Sets of Python. 

Let us consider three sets, X, Y, and Z.  

X = {2,4,6,8,10,12} 
Y = {1,2,3,4,5,6,7,8,9,10} 
Z = {1,3,5,7,9,11,13} 

Commonly Used Python Functions For Data Structure

Union

The Union of sets unites all the elements of multiple sets into one. Let us perform the Union of sets X and Z. The Union is performed with the ‘|’ operator.  

U = X | Z 

Commonly Used Python Functions For Data Structure

We can see, when you check the U set, all the elements from X and Z can be seen now, thus confirming the union of those two sets.  

union() 

We can also perform the union of sets using the union method.  

X.union(Y) 

Commonly Used Python Functions For Data Structure

We can see, all the elements of X and Y are operated with the union.  

Intersection

The intersection of sets produces the set of the common elements of two sets. It is performed using the ‘&’ sign.  

X & Y 

Commonly Used Python Functions For Data Structure

We can see, the common elements of the sets X and Y are obtained.  

intersection() 

The intersection of sets can also be performed with the intersection function.  

Y.intersection(Z) 
Y.intersection(X) 

Commonly Used Python Functions For Data Structure

Thus, in this article, we learned about various functions in Python for different Data Types such as List, Dictionaries, Tuple, and Sets. Understanding Data Types is key as it helps us grasp data structure and is a foundation for any software engineer to dive into deeper complex problems. Practicing these examples will embed the knowledge and thus, would suggest the reader try these out. 


Similar Articles