Understanding the Python Data Structures
Structure: Sets, List, Tuple and Dictionary
is Data Structure?
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:
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.
Update list using append:
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.
items can be access using index numbers.
in the list can be shorted using short function, but with different datatype
list cannot be short.
Deleting Elements in List
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:
Deleting Elements in List using Remove:
Deleting Elements in List using Pop:
If you want to delete the complete list then
del is used for it.
In output it shows error because list is completed deleted. So, it can’t print
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.
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.
can be easily accessible using index numbers.
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.
A tuple's elements cannot be changed, as previously stated. We can't delete or
remove entries from a tuple because of this.
keyword del on the other hand, can be used to completely delete a tuple.
In output it shows error because tuple is completed deleted. So, it can’t print
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.
Note: your output may differ because there is no fix order of set elements
Sets are not accessible because index of element is not fixed in the data.
Once set is created you cannot make changes
into it but new items can be added using add function. Let’s understand it by
Adding new element in set
more way is there to add elements in the set, by using update function.
Remove Specific Element in Set:
element can be removed from the set using remove function.
Because the set has been completely erased, there is an error in the output. As
a result, it is unable to print the output.
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
access the value of a dictionary element, you can use the traditional square
brackets with the key.
can add more elements or key-value pair in the dictionary with the help of
Simply use the del statement to erase a entire
- Avani Popat
- Mar, 04 2022