DATA STRUCTURES IN PYTHON

Sandhiya M
4 min readApr 6, 2022

--

In this article, we will discuss the Data Structures in the Python Programming Language and how they are related to some specific Python Data Types. We will discuss all the in-built data structures like list tuples, dictionaries, etc.

Data structures

Data Structures are a way of organizing data so that it can be accessed more efficiently depending upon the situation. Data Structures are fundamentals of any programming language around which a program is built. Python helps to learn the fundamental of these data structures in a simpler way as compared to other programming languages.

1.Lists in Python

Python Lists are just like the arrays, declared in other languages which is an ordered collection of data. It is very flexible as the items in a list do not need to be of the same type.

Real time- data that are to be stored for for a little while, which includes address change in aadhar, marks change in evaluation.

Empty list- creating a blank list — -> a=[]

  1. Appending- adding up the elements to the end.

Syntax- list.append(value)

2. Pop()- Removes last element unless mentioned.

Syntax- list.pop(parameter)

3. Remove()- removes the specified value.

Syntax- list.remove(element), where element contains the arguments to be removed and takes up single value.

4. Count()- Occurrences of the element.

Syntax- list.count(element)

5. Sort()- arranges the elements in ordered sequences either in ascending or descending order.

Syntax- list.sort()- empty sort denotes ascending order.

Ascending sort
Descending sort

6. Copy()- Stores the original data in different variable, with the means to avoid changeable and altered data.

Since, the list is mutable, when data is updated or modified, the original data gets altered.

Syntax- list.copy()

2: Tuples in Python

Immutable- data can’t be altered or changed.

Ordered sequences and holds on different data type.

Supports indexing and slicing.

tuple===> ()

Indexing- positioning the location, starts from 0

Slicing- getting the elements from given range.

3: Dictionary in Python

Data values are stored in key:value pairs using dictionaries. A dictionary is an ordered, changing collection that does not allow duplication.

Mutable Array- changeable

Indexing through keys

Unordered elements

dict — -> {}

4: Set in Python

Multiple items can be stored in a single variable using sets.

Mutable- can change the content as and when needed.

Doesn’t support indexing.

Contains only unique element.

Deletes duplicate items.

  1. Pop()- Deletes random number from the set.

There is no way of knowing which item will be popped because set is an unordered data type. It’s totally arbitrary.

Syntax- set.pop()

Random Number deleted

2. Remove()- removes specified number.

Syntax- set.remove(value)

3. Clear()- remove all the items from a set.

Syntax- set.clear() — has no parameter

4. copy()- Returns a copy of the set.

Syntax — set.copy()

no parameters

So these are the basics of Data structures. Will catch you all soon in the next blog..!

--

--