This article is a continuation of Python 101: Introduction to Modern Python where we introduced, set up and gave some basic understanding of the python. In this article we will discuss about the various data structures in Python such as lists, dictionaries, tuples, sets, queues, stack, linked list etc.
What is a data Structure?
This is a particular way of organizing data in computer so that it can be accessed/used/processed effectively.
What is a data Algorithm?
This is a finite sequence of steps/instructions on how to perform a task, run an obligation or solve a problem.
Its Important in that it can enable one to estimate the amount of resources that will be needed during implementation.
Types of Data Structures in Python
Data structures in python are divided into 2:
-
Inbuilt data structure:
These types of data structures include
lists,dictionaries,tuples,sets. -
User-defined Data structures:
These types of data structures includes
queues,stack,linked list,tree,linked-list,graphandHashMap.
They can also be categorized into linear or non-linear data structures where in Linear data structures the arrangements of data is in sequence manner e.g List, Linked-list, Queue, Stack etc while in Non-linear structures one element or node is connected to 'n' number of elements e.g tree and graphs.
List
Python Lists are just like the arrays, declared in other languages which is an ordered collection of data. It is very flexible as the items in a list do not need to be of the same type.
The implementation of Python List is similar ArrayList in JAVA. The costly operation is inserting or deleting the element from the beginning of the List as all the elements are needed to be shifted.These two operation usually takes order on n, i.e O(n) meaning the larger the size the longer the time taken.
Example of a list in Python is as shown below.
Python 3.9.9 (main, Dec 16 2021, 23:13:29)
[GCC 11.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> mylist = ["Jeff", 44, 2.3, "Ous"]
>>> print(mylist)
Output
['Jeff', 44, 2.3, 'Ous']
List elements are accessed by the assigned index.Starting index of the list is 0 and the ending index is n-1 where n is the number of elements.
E.g to access Jeff in the above example we use 0
print(mylist[0]) this will output Jeff and print(mylist[2]) will output 2.3.
Adding Elements
Adding the elements in the list can be achieved using the append(), extend() and insert() functions.
The append() function adds all the elements passed to it as a single element.
The extend() function adds the elements one-by-one into the list.
The insert() function adds the element passed to the index value and increase the size of the list too.
mylist.append(12)
mylist.extend("Smart")
mylist.insert(1, "Lux")
print(mylist)
Output:
['Jeff', 'Lux', 44, 2.3, 'Ous', 12, 'S', 'm', 'a', 'r', 't']
Other methods associated with List are del() for deletion, len() function returns the length of the list,index() function that finds the index value of value passed where it has been encountered, count() function finds the count of the value passed to it, sorted() and sort() functions sort the values of the list and sorted() has a return type whereas the sort() modifies the original list.
Dictionaries
Dictionaries are used to store key-value pairs. It is an unordered collection of data values, used to store data values like a map. Key-value is provided in the dictionary to make it more optimized.
Indexing of Python Dictionary is done with the help of keys. These are of any hashable type. We can create a dictionary by using curly braces ({}) or dictionary.
Sample Code:
Python 3.9.9 (main, Dec 16 2021, 23:13:29)
[GCC 11.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> dictionary = {1: "Jeff Odhiambo",2:"Lux Academy",3:"Laurent Ous"}
>>> print(dictionary)
Output:
{1: 'Jeff Odhiambo', 2: 'Lux Academy', 3: 'Laurent Ous'}
Accessing individual elements in a dictionary we can use the key e.g we can use 1 to access Jeff Odhiambo e.g run print(dictionary.get(<key>)) i.e print(dictionary.get(1)) will print Jeff Odhiambo,print(dictionary.get(2)) will print Lux Academy.
>>> print(dictionary.get(1))
Jeff Odhiambo
>>> print(dictionary.get(2))
Lux Academy
>>> print(dictionary.get(3))
Laurent Ous
>>>
