Data Structures and Algorithms Every Python Programmer Should Know (Part I)

Rakshith
Python in Plain English
5 min readMay 17, 2021

--

Photo by David Clode on Unsplash

For solving real-world problems in coding, everyone is looking for runtime efficiency as well as resource efficiency.

A data structure is a data organization, management, and storage format that enables efficient access and modification. Knowing which data structures to be used for solving a current problem will increase the runtime and resource efficiency. For this reason, all the top industries and companies look for people with a strong understanding of data structures.

Here’s what we’ll be looking at today:

  • List
  • Tuples
  • Sets
  • Dictionaries

The data structures in Python are modified forms of the basic data structures that use built-in structures as their backbone, namely:

Basic Data Structures in Python
  • List: It is an array-like data structure that lets us save mutable data objects of the same type as a variable. They are declared within [].
List = ["car", "bike", "auto"]
  • Tuple: Tuples are immutable lists which means once the data is entered, it cannot be changed. They are declared within ().
Tuple = ("car", "bike", "auto")
  • Sets: Sets are unordered collections of data objects that are declared within {}.
Set = {"car", "bike", "auto"}
  • Dictionaries: Similar to hashmaps in other languages, a dictionary is a collection of key-value pairs declared within {}.
Dictionary = { "brand":"Ford" , "model":"Mustang", "year": 2000 }

Now let’s go through them one by one.

Arrays (Lists) in Python:

Python does not have a built-in array type, but you can use lists for all of the same tasks. An array is a collection of values of the same type under the same name.

Arrays in Python

Unlike the static arrays in Java Programming Language, arrays in python can be automatically scaled up or down by adding or deleting elements. There are numerous functionalities of a list. Here are some of them:

  • len(): This function gives the length of the list (array).
  • append(element), insert(index, element) , old_list.extend(new_list) : These functions are used to insert an element into the list. The append function adds the element to the last position of the list, the insert function adds the element to the index that is provided and the extend function adds a new list to an existing old list or vice versa.
  • remove(element) , pop(index) , del list[index] or del list , list.clear() : These functions are used to either remove certain elements of the list or the list itself. The remove function removes the specified element from the list. The pop function removes the element from the specified index and the del function deletes the given element or the list itself. The clear function is a little different, this function clears all the elements in the list but keeps the list intact without any elements.
  • list.sort() , list.sort(reverse=True) : These functions can be used to sort the given list in either ascending or descending order.
  • list.copy() : This function creates a copy of the list at that instance of time.

The most fascinating thing about Python lists is List Comprehension. It offers a shorter syntax for creating a new list based on an existing list or a completely new list. Here’s an example

number_list = [ x for x in range(20) if x % 2 == 0]
# This list will contain a list of even numbers starting from 0 upto 20

Tuples in Python:

Tuples are basically immutable lists. They are declared with round brackets () . The functionalities of tuples are as follows:

A simple tuple representation
  • len() : This function gives the length of the data structure.
  • To access the elements of the tuple we use tuple indexing which can be tuple[1] or tuple[-1] for last element and a range of indexes namely tuple[1:3] .
  • Updating a tuple is not possible since they are immutable, but there is a workaround which is converting the tuple into a list, changing the list and then converting it back into a tuple.
  • We can join 2 tuples to create a new tuple by using + operator.
tuple1 = ("a","b","c")
tuple2 = (1, 2, 3)
tuple3 = tuple1 + tuple2
  • tuple.count(element) : This function returns the number of times a specified value occurs in a tuple.
  • tuple.index(element) : This function searches the tuple for a specified value and returns the position of where it was found.

Sets in Python:

A set is a collection of data objects which is both unordered and unindexed. It is declared within {} . The functionalities are as follows:

Sets in Python
  • len() : This function gives the length of the data structure.
  • set.add(element) : This function adds the element to the set.
  • oldset.update(newset) : This function appends the new set to an existing old set.
  • set.remove(element) ,set.discard(element), set.pop(): These functions can be used to remove an element from the set.
  • set1.difference(set2) : This function returns the difference of two sets.
  • set1.intersection(set2) : This function returns the intersection of two sets.
  • set1.union(set2) : This function returns the union of two sets.
  • set1.symmetric_difference(set2) : This function returns the symmetric_difference of two sets.
  • set1.isdisjoint(set2) , set1.issubset(set2) , set1.issuperset(set2) : These functions return True or False based on the condition that it validates.

Dictionaries in Python:

Dictionary is a collection of data objects stored in the form of key-value pairs. They are ordered and are mutable. They are declared by {} .

The functionalities of the dictionaries are as follows:

  • len() : This function gives the length of the data structure.
  • dictionary.copy() : This function returns a copy of the dictionary.
  • dictionary.fromkeys(keys, value) : This function returns a dictionary of the given keys with the value value .
  • dictionary.get(key), dictionary.items() : These functions return the value of the key specified and a list containing tuples of key-value pairs.
  • dictionary.pop(key), dictionary.popitem() : These functions remove an item from the dictionary based on the key or the last key-value pair.
  • dictionary.values() : This function returns all the values of the dictionary.

That’s it folks for this part of the post. Come back later for more on Python. Peace out.

The second part of this post is coming up!

Read the second part HERE

--

--