Understanding the Python Data Structures
Data Science

Understanding the Python Data Structures


Python Data Structure: Sets, List, Tuple and Dictionary

 

What is Data Structure?

Data structure is way use to organise the data within its group and type. In python language there are four types of data structure. Each of one is used to store the collected data but with different usage. They are as follow:

1)     List

2)     Tuple

3)     Set

4)     Dictionary

 

LIST

 List is the most functional variable among all datatype in Python because list can be created with different datatype item also. List is written between square brackets.

 

Creating List:

For Example-

 

 

Update list using append:

In list more items can be added but as the order of old item is fixed. So, new items will always result in the last of list. For adding new items append function is used. Index in the list starts from zero then one and so on.

For Example-

 

Accessing list

List items can be access using index numbers.

For Example-

 

  

Sorting List

Items in the list can be shorted using short function, but with different datatype list cannot be short.

For Example-


Deleting Elements in List

Elements inside list can be deleted using remove function or even using pop method. If you want to delete the list then delete is used. For deleting the specific element inside the list there are two ways:

1)     Remove

2)     Pop

 

Deleting Elements in List using Remove:

For Example-

 

Deleting Elements in List using Pop:

For Example-

 

  

 

Deleting list

If you want to delete the complete list then del is used for it.

For example-

 

Note: In output it shows error because list is completed deleted. So, it can’t print the output.

 

TUPLE

Tuple is also a collection of elements like list but, once tuple is created you cannot add any other elements in it. It is changeless. Tuple is written in parentheses.

Create Tuple

For example:

 

Update tuple:

As we discuss that tuple is immutable. So, if you want to add any other elements in the tuple then you cannot use append function directly to tuple instead of that you have to convert tuple into list then use the append and after that you can add the element into it.

For example-

 

  

Accessing tuple:

Tuple can be easily accessible using index numbers.

For example-

 

  

Adding new tuple:

If something goes wrong in elements of tuple, we cannot make changes in it. But if you want add new tuple you can easily add it using + operation.

For example-

 

Note: A tuple's elements cannot be changed, as previously stated. We can't delete or remove entries from a tuple because of this.


Deleting tuple:

The keyword del on the other hand, can be used to completely delete a tuple.

For example-



Note: In output it shows error because tuple is completed deleted. So, it can’t print the output.

 

SET

Set is also collection of data but order of the elements is not fixed and immutable also. Set doesn’t allow to duplicate values. Set is written in curly brackets.

 

Creating Set:

For example-

 

 


Note: your output may differ because there is no fix order of set elements

Accessing set:

Sets are not accessible because index of element is not fixed in the data.

Update set:

Once set is created you cannot make changes into it but new items can be added using add function. Let’s understand it by example.

Adding new element in set

For Example-

 

One more way is there to add elements in the set, by using update function.

For Example-

 


Remove Specific Element in Set:

Specific element can be removed from the set using remove function.

For Example-

 

 

Deleting set:

For Example-

Note: Because the set has been completely erased, there is an error in the output. As a result, it is unable to print the output.

  

DICTIONARY 

 

Dictionary is use for the storing for data. Elements inside data will be in sequence and you can change the order of it if required. Duplicates are not allowed in dictionary. It is written in a way that colon (:) separates each key from its value, commas divide the elements, and the whole thing is enclosed in curly braces.


Creating Dictionary:

For example-


 

Accessing Dictionary:

To access the value of a dictionary element, you can use the traditional square brackets with the key.

For example-

Update Dictionary:

You can add more elements or key-value pair in the dictionary with the help of update command.

For example-

 

 

Deleting Dictionary:

Simply use the del statement to erase a entire dictionary

For example-

  • Avani Popat
  • Mar, 04 2022

Add New Comments

Please login in order to make a comment.

Recent Comments

Be the first to start engaging with the bis blog.