DEV Community

fridahmbithe54
fridahmbithe54

Posted on

Introduction to data structures and algorithms

In this article we shall be discussing various data structures in python such as Lists, dictionaries, tuples, sets, queues, stack and linked lists. A data structure refers to a named location which is used for organizing, managing and storage of data according to their data types. A data algorithm refers to the steps or instructions undertaken with the sole purpose of solving a particular problem.

Types of data structures

Data structure are divide into two i.e. Built-in data structures and user defined data structures.
A diagram showing the different types of data structures and their subdivisions

  • Built-in data structures Refers to data structures that allow a programmer to organize, store and manages data to easily allow data access and modification when need arises. They include; lists, dictionaries, tuples and sets.

Lists

A list refers to an ordered collection items which can be used in creating objects of a particular type. List are always put in between square brackets and separated by commas.
Example of a list;

fruits = ['mango', 'banana', 'apple', 'grapes']
print(fruits)
Enter fullscreen mode Exit fullscreen mode

output

['mango', 'banana', 'apple', 'grapes']
Enter fullscreen mode Exit fullscreen mode

List elements are accessed using their assigned indexes starting with 0 which is the first element in the list and ending with n-1 where n is the total number of the elements.
example;

print(fruits[0])  # prints the element at index 0
print(fruits[0::])  # prints the whole list
print(fruits[2])  # prints the element at index 2

Enter fullscreen mode Exit fullscreen mode

output:

mango
['mango', 'banana', 'apple', 'grapes']
apple
Enter fullscreen mode Exit fullscreen mode

Adding elements into a list

It can be achieved by using append() which adds all the passed element as a single element, extend(), add single elements at a time and insert() functions, adds the element passed to the index value and increase the size of the list.
Example;

fruits.append(['watermelon', 'pineapples', 'pawpaw'])
print(fruits)
fruits.extend(['peas', 'pawpaw'])
print(fruits)
fruits.insert(2,'oranges') # 2 shows the index number where the item should be added in the list
print(fruits)
Enter fullscreen mode Exit fullscreen mode

output

['mango', 'banana', 'apple', 'grapes', ['watermelon', 'pineapples', 'pawpaw']]
['mango', 'banana', 'apple', 'grapes', ['watermelon', 'pineapples'], 'peas', 'pawpaw']
['mango', 'banana', 'oranges', 'apple', 'grapes', ['watermelon', 'pineapples'], 'peas', 'pawpaw']

Enter fullscreen mode Exit fullscreen mode

Dictionaries

They are used to store key-value pairs. They can be created either using Cali brackets {} or function brackets ().
Example;

my_stuff = {'lunch': 'burger', 'breakfast': 'Toast'}
print(my_stuff['lunch'])
my_stuff['dinner']= 'pasta'
print(my_stuff['dinner'])
print(my_stuff)
Enter fullscreen mode Exit fullscreen mode

output

burger
pasta
{'lunch': 'burger', 'breakfast': 'Toast', 'dinner': 'pasta'}

Enter fullscreen mode Exit fullscreen mode

Changing and adding key value pairs

my_stuff['dinner']= 'pasta'
print(my_stuff['dinner'])  # adding key-value pair
my_stuff['lunch'] = 'pilau'  # changing key-value pair
print(my_stuff)

Enter fullscreen mode Exit fullscreen mode

output

pasta
{'lunch': 'pilau', 'breakfast': 'eggs', 'dinner': 'pasta'}

Enter fullscreen mode Exit fullscreen mode

Tuples

They are similar to lists are with the exception that the data once entered into the tuple cannot be changed no matter what.
Example;

my_tuple = (1, 2, 3)
print(my_tuple)
my_tuple = my_tuple + ('python', 'java')  # appending elements in a tuple
print(my_tuple)
Enter fullscreen mode Exit fullscreen mode

output

(1, 2, 3)
(1, 2, 3, 'python', 'java')

Enter fullscreen mode Exit fullscreen mode

accessing elements in tuples is just like in list by using the elements index i.e.

print(my_tuple[3])
Enter fullscreen mode Exit fullscreen mode

output

python
Enter fullscreen mode Exit fullscreen mode

Sets

Sets are a collection of unordered elements that are unique in that even if data is entered in more than one time it will only appear once in the set. For example;

x = {1,2,2,2,3,3,3,4,5}
print(x)

Enter fullscreen mode Exit fullscreen mode

output

{1, 2, 3, 4, 5}

Enter fullscreen mode Exit fullscreen mode
  • User-defined data structures These are data structures that aren't supported by python but can be programmed to reflect the same functionality using concepts supported by python are user-defined data structures. They include; queues, stack, linked lists, trees, Hashmaps and graphs.

Queues

They are linear structure that allows insertion of elements from one end and deletion from the other. They use First In First Out(FIFO) methodology for their operations. The end which allows deletion is known as the front of the queue and the other end is known as the rear end of the queue.

Tree

An hierarchical data structure with the top most part of it being the root i.e the starting point and the finishing points are called leaves. They are used for the storage of non linear data with hierarchical structure.

Graph

Refers to a non-linear data structure which is made up of nodes and edges.

Hashmap

Are indexed data structures that use hash functions to compute an index with a key into an array of slots. Its value is mapped to the slot with the corresponding index. The key is unique and immutable. In Python, dictionaries are examples of hash maps.

Top comments (0)