Python Data structure

By | October 31, 2020
Python Data structure

Whenever you are going to attend a tech job interview, the interviewer will surely ask a ton of questions from the data structure. This is because data structures are the basic concept of every programming language and if you are not aware of these basics you will definitely face difficulty to tackle the interview. In this article, we will cover all the basics of Python data structure. In every programming language, you will find many data structures some of those are an inbuilt data structure and some are a user-defined data structure. The user-defined data structures are those data structures which are defined by the user such as stack, queue, linked list etc. and the built-in data structures are arrays, list, dictionaries, sets, tuples, etc. Though Python does not have a built-in concept of the array, you can import it using the numpy package. In python, you will find some built-in data structures you have never seen in other programming languages such as list, dictionaries, tuples, and sets.

Vamware

What is Data Structures?

A data structure in the simple language is “data Structures are the different way of the programming languages of storing data on your computer.” Data structure matters a lot and the main concern of data structure is how to store data in that way so it can be retrieved very quickly and efficiently. It basically defines the relationship between the data and the different operations performing on the data.

Data Structure can be classified in many ways but the 2 main entities on which data structure are defined are Primitive data types and Non-primitive data types.

Primitive Data Structures in Python In-built Non-Primitive Data Structure in Python
Integers List
Float Tuples
String Dictionaries
Boolean Sets

In this article, we will discuss the Python In-built Non-Primitive Data Structure.

List

Definition

Lists are the most widely used data structure in Python and they look similar to Array. The main difference between array and the list is, arrays are the collection of Homogeneous elements whereas List is the collection of both homogeneous and heterogeneous elements. If we go for a more specific definition of the list, this could be that a list is a collection of heterogeneous elements.

In Python, the bracket plays an important role when you define a list. To declare a list all the elements should be inside the sq. brackets.

As like arrays, on the list, we use indexing to retrieve a value. In the list, we can pass the negative index to reverse the list.

# Syntax of list

List1= [ 1 ,” hello ”, ”world” ,True ]
List2 =list(range(10))
print(List1)
print(List2)
print(List1[0]) # Indexing
print(List2[2:6]) # list slicing
print(List1[-2]) #Negetive Indexing

#OUTPUT:
[ 1 ,” hello ”, ”world” ,True ]
[0,1,2,3,4,5,6,7,8,9]
1
[2,3,4,5]
“world”
There are various methods that can be applied on a list. Some of the most important methods are:

  • append()
  • extend()
  • insert()
  • remove()
  • index()
  • count()
  • pop()
  • reverse()
  • sort()
  • copy()
  • clear()
  • any()
  • all()
  • ascii()
  • bool()
  • enumerate()
  • filter()
  • iter()
  • list()
  • len()
  • max()
  • min()
  • map()
  • reversed()
  • slice()
  • sorted()
  • sum()
  • zip()

Dictionaries

Dictionaries are the unordered key: value pairs data structure in Python. Many programming languages have the concept of the associative array which look like dictionaries. In dictionaries, each key is associated with a value and the keys are immutable and unique but the value can be changed.

To define dictionaries we declare a pair of key and value and they should be separated by a : (colon) and after defining a pair of key and value use, (comma) to define next key: value pair.

Dictionaries use curly braces to signify the key and their associative values.

#Dictionaries Syntax

dict_1= { “key1”:”value1” , “key2:”value2”, “key3”:”value3” , “key4”:”value4”}
print(dict_1)

#OUTPUT

{“key2”:”value2” , “key1:”value1”, “key4”:”value4” , “key3”:”value3”}

As in the above code, you can see that the output of the code is not in that sequence in which it had been declared, this is because dictionaries are the unordered data structure.

As a like list, dictionaries also use indexing to call the values but in dictionaries, we use keys to grab its corresponding values.

# Syntax to grab the value using index or key.

Dic={1:2, 3:4 ,”hello”: world}
print(Dic[“hello”])

#OUTPUT

“world”

Dictionaries methods:

  • clear()
  • copy()
  • fromkeys()
  • get()
  • items()
  • keys()
  • popitem()
  • setdefault()
  • pop()
  • values()
  • update()
  • any()
  • all()
  • ascii()
  • bool()
  • dict()
  • enumerate()
  • filter()
  • iter()
  • len()
  • max()
  • min()
  • map()
  • sorted()
  • sum()
  • zip()

Tuple

A tuple is similar to list but has some differences. The main difference is that tuples are immutable and to declare the tuple we use parenthesis () instead of sq. brackets []. The immutability of the tuples signifies that once an element is inside the tuple has been declared, it can not be reassigned.

#Basic syntax of Tuple:

Tup=(1,2,3,4,5)
print(Tup)
print(Tup[2]) # as like list tuple uses indexing to grab the value.

#OUTPUT

(1,2,3,4,5)
3

Why use Tuples?

  • Tuples are faster than list.
  • Often, we prefer that data structure whose value could not be manipulated.
  • Not to override the declared values by mistake.

You can convert a list into a tuple by using tuple() inbuilt function, it is an easiest and shortest way of conversion.

#Convert a list into a tuple

li=[1,2,3,4]
tu=tuple(li)
print(type(tu))

#Output

<class_ tuple>

Tuples methods:

  • count()
  • index()
  • any()
  • all()
  • ascii()
  • bool()
  • enumerate()
  • filter()
  • iter()
  • len()
  • max()
  • min()
  • map()
  • reversed()
  • slice()
  • sorted()
  • sum()
  • zip()

Sets

Definition of a set is “Sets are the collection of unordered unique elements”. The unique element state that all the elements in a set are different. Sets are similar to the list because set is mutable and represent inside the sq. brackets the only difference is that in a set you cannot store two same value.

#Syntax to implement a Set

Lis=[1,2,3,3,4,5,5,5,6,6,6] # it’s a list having two 3s
print(Lis)
se=set(Lis) # here we have use set function to convert a list to a set
print(se)

#Output

[1,2,3,3,4,5,5,5,6,6,6]
[1,2,3,4,5,6]

In the above code the when we print the Lis it prints all the elements include the repetitive once but when we print se it prints only all elements except the repetitive elements.

Set methods:

  • count()
  • index()
  • any()
  • all()
  • ascii()
  • bool()
  • symmetric_difference()
  • symmetric_difference_update()
  • union()
  • update()
  • issubset()
  • disjoint()
  • intersection_update()
  • discard()
  • enumerate()
  • filter()
  • iter()
  • len()
  • max()
  • min()
  • map()
  • reversed()
  • slice()
  • sorted()
  • sum()
  • zip()

Python Tutorials:

Introduction

Flow Control

Functions

Datatypes

File Handling

Object & Class

Additional Tutorials

Leave a Reply

Your email address will not be published. Required fields are marked *