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.
- 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)
output
['mango', 'banana', 'apple', 'grapes']
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
output:
mango
['mango', 'banana', 'apple', 'grapes']
apple
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)
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']
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)
output
burger
pasta
{'lunch': 'burger', 'breakfast': 'Toast', 'dinner': 'pasta'}
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)
output
pasta
{'lunch': 'pilau', 'breakfast': 'eggs', 'dinner': 'pasta'}
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)
output
(1, 2, 3)
(1, 2, 3, 'python', 'java')
accessing elements in tuples is just like in list by using the elements index i.e.
print(my_tuple[3])
output
python
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)
output
{1, 2, 3, 4, 5}
- 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)