0% found this document useful (0 votes)
4 views10 pages

Data Structures in Python

Uploaded by

shitij.sh20001
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views10 pages

Data Structures in Python

Uploaded by

shitij.sh20001
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 10

Data Structures in Python

Exploring efficient data organization and management in Python.

by Shitij, Naman and Kashish


What is a Data Structure?
Data Structure is a process of manipulates the
data. Manipulate is a process of organizing,
managing and storing the data in different ways.
It is also easier to get the data and modify the
data. The data structure allows us to store the
data, modify the data and also allows to compare
the data to others. Also, it allows performing some
operations based on data.
Classification of Data Structures
in Python

Built-in Data Structures: These are pre-defined structures in Python


that facilitate efficient data manipulation and storage.

User-Defined Data Structures: These are custom-built data structures


created by programmers to efficiently store, manage, and manipulate data
based on specific application needs.
Built-in Data Structures
Python having some implicit data structure concepts to access and store the data. The
following are the implicit or Built-in Data structures in python.

List
Ordered, mutable collections that allow duplicate elements.

Tuple
Ordered, immutable collections, often used for fixed data sets.

Dictionary
Unordered collections of key-value pairs, providing fast lookups.
List in Python

A list is a built-in data structure in Python that stores an ordered collection of


elements.

 An ordered sequence supporting duplicate elements.

 Implements index-based storage for direct element access.

 Accommodates heterogeneous data types.

 Mutable, permitting modification of elements post-creation.


Creating a List
Code Output
my_list1 = []
print(my_list1) []

[1, 2.5, 'hello’]


my_list2 = [1, 2.5, 'hello’]
print(my_list2) [1, 2, 3, 4, 5]

my_list3 = list([1, 2, 3, 4, 5])


print(my_list3)
Adding Elements to List
append()
Adds an element at the end of the list. This method modifies the list in place and is useful for adding a
single item to the end of an existing list.

insert()
Adds an element at a specified index within the list. This allows for precise placement of new items at
any point in the list, shifting subsequent elements to accommodate the new entry.

Example: Output:
my = []
[10,30,20]
my.append(10)
my.append(20)
my.insert(1, 30)
print(my)
Removing Elements from List
remove() pop() clear()

Removes the first instance of a Removes the element at a specific Removes all elements from the list,
given value. Raises a ValueError if index and returns it. Removes the leaving it empty but still an
the value is not found. last element if no index is given. existing object.
Accessing List Elements
l1 = [1, 2, 3, 4, 5]
print(l1[3]) # Returns 4
print(l1[:]) # Returns entire list
print(l1[1:]) # Returns from index 1 onwards
print(l1[:3]) # Returns up to index 3
Dictionary in Python
Key-Value Pairs Unique Keys
Data is stored in pairs of keys and values, allowing Each key must be unique. If you add the same key
quick organization and access. Keys must be twice, the old value will be replaced. This ensures
unchangeable (like strings or numbers), while values each key points to only one value.
can be any data type.

Useful for Identification Mutable Structure


Dictionaries are great for quick identification. You You can change dictionaries after creating them.
can easily find information using a unique identifier Add, remove, or update key-value pairs as needed.
like a user ID, making data access very fast. This makes dictionaries flexible for managing
changing data.

You might also like