Built-in Data Structures using Python

In this blog, we will see about Data structures using Python. Friends, Data structure is a very crucial topic in any programming language. So, it is very important to clear the concept of data structure in Python.
As we know that Python is a high-level and object-oriented language. There are 2 types of data structures in Python programming.
  • Built-in Data Structure
  • User-Defined Data Structure
This blog is about Built-in Data Structure using Python.

Built-in data structures in Python are those type which are specific to Python programming. 

Below are the built-in data structure in Python 

  • List
  • Tuple
  • Set
  • Dictionary


Python List

List is similar to array with one major difference. The difference between array and list is that a list can have data with different data types. It means a single List in Python can have number, string and boolean as well.

MyList = [1,2,3,'A','Python',True] 
print(MyList)

Count an Item from a List in Python

print(MyList.count('A'))

Above code will return number of A exist in MyList.

Reverse a List item in Python

MyList.reverse()

Append new item in a Python List


MyList.append('New Item')

To Pop / Remove an item from a List in Python

MyList.pop(index number)
MyList.pop(1) #It will remove item which is at index 1 in MyList

Python Tuples 

Tuples in Python are similar to Python List but the difference is that Python tuples are not mutable. Meaning that, once item inserted in a Tuple, it can not be modified later.

Syntax to define a  Tuple in Python -

MyTuple = (item1, item2, item3......)

MyTuple = (1,2,3)
print(MyTuple)

As you know now that, Python tuples can not be modified.
So, let's try to modify an item in Tuple.

MyTuple = (1,2,3)
MyTuple[0] = 5

In above code, I am trying to modify the item at index 0 (zero) in Tuple, but it will throw an error if I will run this code, because Python tuples are not mutable.

Error - "TypeError: 'tuple' object does not support item assignment"

Python Dictionary

Dictionary is a built-in data structure in Python which mainly stores data in Key, Value pair.

Syntax to define a Dictionary in Python -

MyDictionary = {1:"Item1",2:"Item2"}
print(MyDictionary)

Insert new item in Dictionary

Synatx to insert new item in Dictionary 
MyDictionary[Key] = Value;

Now, suppose we have below dictionary -

MyDictionary = {1:"Book1",2:"Book2"}

Now, we want to add new item with Key = 3 and value = 'Book3'

So, as per syntax -

MyDictionary[3] = 'Book3'

Pop/Remove an item from Dictionary in Python


Syntax to remove an item from a Dictionary.

MyDictionary.pop(KeyName)

So, to delete Book3 from above Dictionary, we can write below line in Python -

MyDictionary.pop(3)

So, above line of code will remove an item from MyDictionary where KeyName = 3

Python Set

Set in Python is also an built-in data structure type which stores data in an unordered fashion.
With set, we can find whether it is a subset of a particular set, we can apply union intersection between two sets.

Syntax to define Set in Python


MySet = {item1, item2, item3,....}

Add a new item in Set

Syntax to add new item in Set

MySet.add(Item)

Union

Union is a operation that can be applied on two sets, which returns items from both the sets.

Syntax to use Union in Python

MySet1 = {1,3,5,7,9}
MySet2 = {2,4,6,8,10}
print(MySet1.union(MySet2))

Output = {1,2,3,4,5,6,7,8,9,10}

Intersection

Intersection is a operation that returns common item from two sets.

Syntax to use Intersection in Python

MySet1 = {1,2,3,4,5,6}
MySet2 = {2,4,6,8,10}
print(MySet1.intersection(MySet2))

Output = {2,4,6}


Summary -

There are two types of data structure in Python, Built-in data structure and User defined data structure.
List, Dictionary, Tuple and Set are built-in data structure in Python.
We have built-in methods to perform insert, remove, update, revers on these data structure.
Hope you like this blog on built-in data structure using Python.

Please share this blog within your group and write your feedback in comment box.

Previous Blog - Modules in Python

Comments