Python — Data Structures

Vasu Devan S
4 min readOct 14, 2022

What is a Data Structure?

The basic Python data structures in Python include list, set, tuples, and dictionary. Each of the data structures is unique in its own way. Data structures are “containers” that organize and group data according to type. The data structures differ based on mutability and order.

What is a List?

List. Lists are used to store multiple items in a single variable. Lists are one of 4 built-in data types in Python used to store collections of data. Lists are ordered collections of objects/elements. the list is mutable. the list can have heterogeneous data types.

Examples:

List_numbers = [5,40,60,58,12,32,52,97,65,68,74]

List_string = [‘Dhoni’, ‘Abhinav’, ‘Neera’, ‘Mirabai’]

List_object =[[2,5,8,9,0],{‘key1’: ‘value1’, ‘key2’: ‘value2’},(8,10,65,20)]

Syntax to create a list in python

It is simple code: List_numbers = [5,40,60,58,12,32,52,97,65,68,74]

Since the list is an ordered data structure we can extract the Elements from their index, the indexing of the list starts from 0 to final position-1, List can also be forward-indexed or reverse-indexed.

Example of calling elements in list and Indexing if list :

What is Dictionary?

Dictionary, Dictionaries are used to store data values in key: value pairs. A dictionary is a collection that is ordered*, changeable, and does not allow duplicates (key). Dictionary is mutable.

Examples:

marks_dict = {‘arun’: [10,5,9,20,25], ‘bindhu’: [12,17,19,9,12], ‘chethan’: [15,12,14,17,16] }

position_dict = {‘Analyst’: ‘Vinay’, ‘TL’: ‘Venkat’, ‘Intern’: ‘Vishal’ }

Dictionary is Key value pair so it is like a list instead of an index we use Key. Dictionary can also contain hetero data types.

Example of calling elements in list and Indexing if list :

What are Tuples?

Tuple. Tuples are used to store multiple items in a single variable. A tuple is one of 4 built-in data types in Python used to store collections of data, A tuple is a collection that is ordered and unchangeable. The major use of Tuple is the Immutable property. if we need to fix data if we store it as a tuple.

Example:

t_numbers = (5,40,60,58,12,32,52,97,65,68,74)

Tuple element/object cannot be updated or deleted once created

What are Sets?

Sets are used to store multiple items in a single variable. Set is one of 4 built-in data types in Python used to store collections of data, the other 3 are List, Tuple, and Dictionary, all with different qualities and usage. A set is a collection that is unordered, unchangeable*, and unindexed.

Example:

set_single = {‘rahul’,57,True,87.95}
set_multi = {‘rahul’,57,True,87.95,’rahul’,57,87.95,True,87.95,’rahul’,57}

As we can see set always Deletes the Duplicate elements from the set.

Hi, all thank you and I hope you got to learn something if you no dude we know all this they are the same data containers in python

I have a Question for you all,

If you feel the Question was interesting please follow and share Thank you

--

--

Vasu Devan S

Data Scientist aspirant | Machine learning | SQL | Web scraping|Data mining|Statistics |Python |Pandas|Advanced Excel