Commonly Used Python Functions For Data Structure

Ojash Shrestha
6 min readJul 11, 2021

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']
#list of numbers
num_list = [9,8,7,6,5]
#list of mixed data types
mixed_list = [3,8,"C# Corner", 'x', 2]

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]

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

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

count()

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

fruit.count('mango')

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

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']
}

keys()

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

fruit_dict.keys()

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

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

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

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

fruit_tuple.clear()

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.

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

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

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

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}

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

Read the Full Article in C# Corner:

--

--