Python Data Stracture
Python Data Stracture
EBook
Copyright: CodewithCurious.com
Copyrighted By @Curious_.programmer
Python Data Structures
Python provides a variety of built-in data structures that can be used to store and
manipulate data. This index page lists the most commonly used data structures in
Python and provides links to detailed explanations and examples.
Lists:
Lists are one of the most versatile data structures in Python, and are used to store
collections of items. Lists can contain elements of different data types and can be
modified after they are created.
Introduction to Lists :
List Methods
List Comprehensions
Tuples :
Tuples are similar to lists in that they can store collections of items, but they are
immutable and cannot be modified once they are created.
Introduction to Tuples :
Tuple Methods
Sets :
Sets are used to store collections of unique items. Sets can be modified after they
are created and support operations such as union, intersection, and difference.
Introduction to Sets
Set Methods
Dictionaries
PDF FILE UPLODED ON TELEGRAM {Link in Bio}
Dictionaries are used to store collections of key-value pairs. Dictionaries are
mutable and can be modified after they are created.
Introduction to Dictionaries
Dictionary Methods
Arrays :
Arrays are used to store collections of homogeneous items, and can be more
efficient than lists for certain types of operations.
Introduction to Arrays
Array Methods
Queues :
Queues are used to store collections of items in a first-in, first-out (FIFO) order.
Introduction to Queues
Queue Methods
Stacks :
Stacks are used to store collections of items in a last-in, first-out (LIFO) order.
Introduction to Stacks
Stack Methods
Linked Lists :
Linked lists are a data structure in which each element (or node) contains a
reference to the next element in the list.
Trees :
Introduction to Trees
Graphs :
Graphs are used to represent networks of nodes and edges.
Introduction to Graphs
This is just a sampling of the many data structures available in Python, but it
should give you a good starting point for learning more about how to use them.
Python provides a variety of built-in data structures, such as lists, tuples, sets, and
dictionaries, that make it easy to organize and manipulate data.
1. Lists are ordered collections of elements that can be modified after they are created.
2. Tuples are similar to lists, but they are immutable, which means that they cannot be
modified after they are created.
3. Sets are used to store collections of unique items, and they support operations such
as union, intersection, and difference.
4. Dictionaries are used to store key-value pairs and allow for efficient lookup of values
based on their associated keys.
5. Arrays are used to store collections of homogeneous items, and they can be more
efficient than lists for certain types of operations.
6. Queues and stacks are used to store collections of items in a first-in, first-out (FIFO)
or last-in, first-out (LIFO) order, respectively.
7. Linked lists are a data structure in which each element (or node) contains a reference
to the next element in the list.
8. Trees are used to store hierarchical data and can be used to represent things like file
systems and organizational charts.
9. Graphs are used to represent networks of nodes and edges, and they are commonly
used in applications such as social networks and transportation systems.
Lists are a commonly used data structure in Python that can store collections of
items. A list is an ordered sequence of elements, and each element can be of a
different data type, such as numbers, strings, or other objects. Lists can be
modified after they are created, which means that you can add, remove, or
modify elements as needed.
List Methods:
Lists have a number of built-in methods that allow you to manipulate the
elements of a list. Some of the most commonly used list methods include:
Here are some examples of how each of the list methods can be used in Python:
List Comprehensions:
List comprehensions provide a concise way to create lists in Python. They allow you to
create a new list by applying an expression to each element of an existing list or other
iterable object. Here's an example of a list comprehension that creates a new list
containing the squares of the numbers from 1 to 10:
This creates a new list called squares that contains the squares of the numbers from 1 to
10. The expression x**2 is applied to each element of the range object range(1, 11),
List comprehensions can also include conditional expressions, which allow you to filter
the elements of an existing list based on some criteria. For example, here's a list
comprehension that creates a new list containing only the even numbers from an existing
list:
This creates a new list called even_numbers that contains only the even numbers from the
list numbers. The expression x for x in numbers is applied to each element of the list, but
only the elements that satisfy the condition x % 2 == 0 are included in the new list. The
result is a new list containing the values [2, 4, 6, 8, 10].
Here's another example of a list comprehension that creates a new list containing the
lengths of strings in an existing list:
List comprehensions are a powerful and concise way to create new lists in Python, and
they are commonly used in a variety of applications.
Introduction to Tuples :
Example:
index():
The index() method is used to find the index of the first occurrence of a specified
element in a tuple.
Example:
Example:
sorted():
The sorted() method is used to get a new sorted tuple from an existing tuple.
Example:
The min() and max() methods are used to get the minimum and maximum value
from a tuple.
Example:
Introduction to Sets:
Set Methods:
add(): Adds an element to the set. If the element already exists in the set, the set
remains unchanged.
Example:
Example:
Example:
pop(): Removes and returns an arbitrary element from the set. If the set is empty, it
raises a KeyError.
Example:
Introduction to Dictionaries:
Dictionaries are a type of collection in Python that allows you to store and
retrieve values based on keys. Each key in a dictionary is unique and associated
In the above example, my_dict is a dictionary with three key-value pairs. The keys
are 'key1', 'key2', and 'key3', and their corresponding values are 'value1', 'value2',
and 'value3', respectively.
Dictionary Methods:
Dictionaries have several built-in methods that you can use to manipulate and
retrieve data from them. Here are some of the most commonly used dictionary
methods:
dict.get(key, default=None): Returns the value associated with the specified key. If
the key is not found, it returns the specified default value.
dict.keys(): Returns a list of all the keys in the dictionary.
dict.values(): Returns a list of all the values in the dictionary.
dict.items(): Returns a list of tuples containing key-value pairs.
dict.update(other_dict): Adds the key-value pairs from another dictionary to the
current dictionary.
dict.pop(key, default=None): Removes the key-value pair associated with the
specified key. If the key is not found, it returns the specified default value.
dict.clear(): Removes all the key-value pairs from the dictionary.
len(dict): Returns the number of key-value pairs in the dictionary.
These are just a few of the methods available for working with dictionaries in
Python. By using these methods, you can easily manipulate and retrieve data
from your dictionaries.
In Python, arrays are a type of data structure used to store homogeneous items.
They can be more efficient than lists for certain types of operations, especially
when dealing with large amounts of numerical data.
In the above code, typecode specifies the type of data that the array will hold,
and [item1, item2, item3, ...] is a list of items to be stored in the array.
Array Methods:
Arrays have several built-in methods that you can use to manipulate and retrieve
data from them. Here are some of the most commonly used array methods:
These are just a few of the methods available for working with arrays in Python.
By using these methods, you can easily manipulate and retrieve data from your
arrays.
A queue is a linear data structure in which items can be added to one end of the
queue and removed from the other end of the queue. This is known as a first-in,
first-out (FIFO) data structure because the first item added to the queue is the
first item removed from the queue.
Queue Methods:
Python has an inbuilt module called queue that provides a Queue class that
implements a queue data structure. The Queue class provides several methods
for adding, removing, and manipulating items in the queue:
Here's an example of how to use the queue module in Python to create and
manipulate a queue:
A stack is a linear data structure in which items can be added or removed only
from one end of the stack, which is called the top. This is known as a last-in, first-
out (LIFO) data structure because the last item added to the stack is the first item
removed from the stack.
Stack Methods:
Python does not have a built-in stack data structure, but you can use a list to
implement a stack. A list provides several methods that can be used to
implement the stack behavior:
pop(): - removes and returns the item at the top of the stack.
[-1]: - returns the item at the top of the stack without removing it.
A linked list is a data structure in which each element, called a node, contains a
value and a reference to the next node in the list. Unlike arrays or lists, linked lists
do not store their elements contiguously in memory, but rather use pointers to
link the elements together.
Linked lists provide several methods for accessing and manipulating their
elements, including:
append(value): - adds a new node with the specified value to the end of the linked
list.
insert(value, index): - adds a new node with the specified value at the specified index
in the linked list.
remove(value): - removes the first node in the linked list that contains the specified
value.
pop(index): - removes and returns the node at the specified index in the linked list.
get(index): - returns the value of the node at the specified index in the linked list.
Here's an example of how to create a linked list in Python and use some of these
methods:
We create a new linked list object, my_list, and add some values to it using the
append method. We then insert a new value at index 1 using the insert method,
remove a value from the list using the remove method, and pop a value from the
list using the pop method.
Finally, we use the get method to retrieve a value from the list at a specified
index, and print the value to the console. This is just a simple example, and there
are many other methods and variations on linked lists that can be used for
different applications.
Traversal refers to the process of visiting each node in a tree. There are several
ways to traverse a tree, including depth-first traversal and breadth-first traversal.
In depth-first traversal, the nodes are visited starting at the root node and
moving down the tree, visiting each child node before moving on to the next
sibling node. There are three types of depth-first traversal: pre-order traversal, in-
order traversal, and post-order traversal.
In pre-order traversal, the current node is visited first, then the left child, and then
the right child. In in-order traversal, the left child is visited first, then the current
node, and then the right child. In post-order traversal, the left child is visited first,
then the right child, and then the current node.
In breadth-first traversal, the nodes are visited starting at the root node and
moving level by level through the tree, visiting all nodes at each level before
PDF FILE UPLODED ON TELEGRAM {Link in Bio}
moving on to the next level. This is often done using a queue data structure to
keep track of the nodes to be visited.
Tree traversal methods are important for many applications, such as searching a
tree for a particular node or calculating the depth of a tree.
In-order traversal is a type of depth-first traversal of a binary tree, in which the nodes
are visited in the following order:
This means that in-order traversal visits the nodes in ascending order. For
example, if we have a binary search tree with the following nodes:
here's an example of a simple binary tree in Python and how to traverse it using
depth-first traversal:
The in-order traversal would visit the nodes in the following order: 1, 2, 3, 4, 6, 8.
Pre-order traversal is a type of depth-first traversal of a binary tree, in which the nodes
are visited in the following order:
This means that pre-order traversal visits the root node first, followed by its left
subtree, and then its right subtree. For example, if we have a binary tree with the
following nodes:
The pre-order traversal would visit the nodes in the following order: 1, 2, 4, 5, 3.
Pre-order traversal can be useful for creating a copy of the tree, as it visits the
nodes in the order in which they would be visited if the tree were being copied
Post-order traversal is a type of depth-first traversal of a binary tree, in which the nodes are
visited in the following order:
This means that post-order traversal visits the left subtree and right subtree before visiting
the root node. For example, if we have a binary tree with the following nodes:
The post-order traversal would visit the nodes in the following order: 4, 5, 2, 3, 1.
Post-order traversal can be useful for deleting a tree, as it visits the leaves of the
tree first before moving on to the root. It can also be used to compute the height
of the tree or to evaluate the expressions in a parse tree, as it visits the operands
before the operators.
We create a binary tree with five nodes, and then traverse it using each of the
three traversal methods. The output of the program will be:
Graphs are a collection of nodes (also called vertices) and edges that connect
these nodes. They are used to model relationships or connections between
objects, such as social networks or transportation systems.
Breadth-first traversal: In this method, the graph is traversed by exploring all the
nodes at a given level before moving on to the next level. This is often used to find
the shortest path between two nodes.