Is a group of data elements that are put together
under one name
Defines a particular way of storing and organizing
data in a computer so that it can be used efficiently
List
Tuples
Talk on Critical Theory, Part II, Philosophy of Social SciencesSoraj Hongladarom
Characteristics, Strengths and Weaknesses of Quantitative Research.pdfThelma Villaflores
How to Create Odoo JS Dialog_Popup in Odoo 18Celine George
How to Set Up Tags in Odoo 18 - Odoo SlidesCeline George
Ad
Python _dataStructures_ List, Tuples, its functions
1. Python Programming – Unit 3
Part – 2
Datastructures
1
https://www.slideshare.net/slideshow/python_funct
ions_modules_-user-define-functions/275958501
Dr.VIDHYA B
ASSISTANT PROFESSOR & HEAD
Department of Computer Technology
Sri Ramakrishna College of Arts and Science
Coimbatore - 641 006
Tamil Nadu, India
3. Python Data Structures
- Is a group of data elements that are put together
under one name
- Defines a particular way of storing and organizing
data in a computer so that it can be used efficiently
4. 1. LIST
- It is a sequence in which elements are written as a
list of comma separated values.
- Elements can be of different data types
- It takes the form
list_variable = [val1, val2, val3,…,valn]
6. Accessing Values in List
- Similar to strings, lists can also be sliced and
concatenated
- square brackets are used to slice along with the
index/indices to get values stored at that index.
-For example:
seq = list[start:stop:step]
seq = list[::2] #get every other element, starting with index 0
seq = list[1::2] #get every other element, starting with index 1
7. Accessing Values in List (Example)
l1 = [1,2,3,4,5,6,7,8,9,10]
print(“List is:”, l1)
print(“First element is:”, l1[0])
print(“Index 2 – 5th
element:”, l1[2:5])
print(“From 0th
index, skip one element:”, l1[ : : 2])
print(“From 1st
index, skip two elements:”, l1[1::3])
Output:
List is: [1,2,3,4,5,6,7,8,9,10]
First element is: 1
Index 2 – 5th
element: [3,4,5]
From 0th
index, skip one element: [1,3,5,7,9]
From 1st
index, skip two elements: [2,5,8]
8. Updating Values in List
- A list can be updated by giving the slice on the left-hand
side of the assignment operator
- New values can be appended in the list with the method
append( ). The input to this method will be appended at
the end of the list
- Existing values can be removed by using the del statement.
Value at the specified index will be deleted.
9. Updating Values in List (Example)
l1 = [1,2,3,4,5,6,7,8,9,10]
print(“List is:”, l1)
l1[5] = 100
print(“After update:”, l1)
l1.append(200)
print(“After append”, l1)
del l1[3]
print(“After delete:”, l1)
del l1[2:4]
print(“After delete:”, l1)
del l1[:]
print(“After delete:”, l1)
Output:
List is: [1,2,3,4,5,6,7,8,9,10]
After update: [1,2,3,4,5,100,7,8,9,10]
After append: [1,2,3,4,5,100,7,8,9,10, 200]
After delete: [1,2,3,5,100,7,8,9,10, 200]
After delete: [1,2,100,7,8,9,10, 200]
After delete: [ ]
10. Slice Operation on List
#insert a list in another list using slice operation
li=[1,9,11,13,15]
print(“Original List:”, li)
li[2]=[3,5,7]
print(“After inserting another list, the updated list is:”, li)
Output:
Original List: [1,9,11,13,15]
After inserting another list, the updated list is: [1,9,[3,5,7],13,15]
11. Nested List
-List within another list
Example:
l1 = [1, ‘a’, “abc”, [2,3,4,5], 8.9]
i = 0
while i<(len[l1]):
print(“l1[“ , i , “] =“, l[i])
i+ = 1
Output:
l1[0] = 1
l1[1] = a
l1[2] = abc
l1[3] = [2,3,4,5]
l1[4] = 8.9
12. Cloning List
Example:
def Cloning(li1):
li_copy = li1[:]
return li_copy
li1 = [4, 8, 2, 10, 15, 18]
li2 = Cloning(li1)
print("Original List:", li1)
print("After Cloning:", li2)
Output:
Original List: [4,8,2,10,15,18]
After Cloning : [4,8,2,10,15,18]
Cloning
If there is a need
to modify a list
and also to keep a
copy of the
original list, then
a separate copy
of the list must be
created
13. Basic List Operations
1 2
Operation len Concatenation
Description Returns length of the list Joins two list
Example len([1,2,3,4,5,6,7,8,9,10]) [1,2,3,4,5] + [6,7,8,9,10]
Output 10 [1,2,3,4,5,6,7,8,9,10]
3 4
Operation Repetition in
Description Repeats elements in the list Checks if the values is present
in the list
Example “Hello”, “World” *2 ‘a’ in [‘a’, ‘e’, ‘i’, ‘o’, ‘u’]
Output [‘Hello’, ‘World’, ‘Hello’,
‘World’]
True
14. Basic List Operations (Continued…)
5 6
Operation not in max
Description Checks if the value is not available
in the list
Returns maximum value in the
list
Example 3 not in [0,2,4,6,8] n=[2,3,4]
print(max(n))
Output True 4
7 8
Operation min sum
Description Returns minimum value in the list Adds the values in the list that
has number
Example n=[2,3,4]
print(min(n))
n=[2,3,4]
print(“Sum:”, sum(n))
Output 2 9
15. Basic List Operations (Continued…)
9 10
Operation all any
Description Returns true if all elements of the
list are true (or if the list is
empty)
Returns true if any elements of
the list are true . If empty False
Example n=[0,1,2,3]
print(all(n))
n=[0,1,2,3]
print(any(n))
Output False True
11 12
Operation list sorted
Description Converts an iterable (tuple,
string, set, dictionary) to a list
Returns a new sorted list. The
original list is not sorted
Example list1=list(“HELLO”)
print(list1)
list1=[1,5,3,2]
list2 = sorted(list1)
print(list2)
Output [‘H’, ‘E’, ‘L’, ‘L’, ‘O’] [1,2,3,5]
16. Basic List Operations (Continued…)
Few more examples on Indexing, Slicing and other
operations
list_a =[“Hello”, “World”, “Good”, “Morning”]
print(list_a[2]) #index starts at 0
print(list_a[-3]) #3rd
element from the end
print(list_a[1:]) # Prints all elements starting from index 1
Output:
Good
World
[ “World”, “Good”, “Morning”]
17. List Methods
1 2
Operation append( ) count()
Description Appends an element to the list Counts the number of times an
element appears in the list
Example n=[6,3,7,0,1,2,4,9]
append(10)
print(n)
n=[6,3,7,0,1,2,4,9]
print(n.count(4))
Output [6,3,7,0,1,2,4,9,10] 1
3 4
Operation index() insert( )
Description Returns the lowest index of obj in
the list
Inserts obj at the specified index
in the list
Example n = [6,3,7,0,1,2,4,9]
print(n.index(7))
n = [6,3,7,0,1,2,4,9]
n.insert(3,100)
print(n)
Output 2 n = [6,3,7,100,1,2,4,9]
18. List Methods (Continued…)
5 6
Operation pop( ) remove( )
Description Removes the element in the specified
index. If no index is specified, last
element will be removed
Removes the specified element
from the list.
Example n = [6,3,7,0,1,2,4,9]
print(n.pop( ))
print(n)
n = [6,3,7,0,1,2,4,9]
print(n.remove(0))
print(n)
Output 9 [6,3,7,0,1,2,4] [6,3,7,1,2,4,9]
7 8
Operation reverse( ) sort( )
Description Reverses the elements in the list Sorts the elements in the list
Example n = [6,3,7,0,1,2,4,9]
n.reverse( )
print(n)
n = [6,3,7,0,1,2,4,9]
n.sort( )
print(n)
Output [9,4,2,1,0,7,3,6,] [0, 1, 2, 3, 4, 6, 7, 9]
19. List Methods (Continued…)
9 Insert()
Remove()
Sort()
The above methods only
modify the list and do not
return any value
Operation extend
Description Adds the element in a list to the end
of the another list
Example n1 = [1,2,3,4,5]
n2 = [6,7,8,9,10]
n1.extend(n2)
print(n1)
Output [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
20. February 23, 2025 20
Using Lists as Stacks
- Stack is a DS which stores
its elements in an ordered
manner.
- Eg: Pile of Plates
- Stack is a linear DS uses the
same principle (ie) elements
are added and removed only
from one end.
- Hence Stack follows LIFO
(Last in First Out)
21. February 23, 2025 21
Using Lists as Stacks
- Stacks in computer science is used in function
calls.
23. February 23, 2025 23
Using Lists as Stacks
- Stack supports 3 operations:
PUSH: Adds an element at the end of the stack.
POP: Removes the element from the stack.
PEEP: Returns the value of the last element from the
stack(without deleting it).
In python list methods are used to perform the above
operation
- PUSH- append () method
- POP- pop() method
- PEEP- slicing operation is used
25. February 23, 2025 25
Using Lists as Queues
- Queue is a DS which stores its elements in an ordered manner.
- Eg:
*People moving in escalator
*People waiting for bus
*Luggage kept at conveyor belt.
*Cars lined at toll bridge.
Stack is a linear DS uses the same principle (ie) elements are
added at one end and removed from other end.
- Hence Queue follows FIFO (First in First Out)
27. February 23, 2025 27
List Comprehensions
Python supports computed lists called List Comprehensions
Syntax:
List= [expression for variable in sequence]
- Beneficial to make new list where each element is
obtained by applying some operations to each
member of another sequence or iterable.
- Also used to create a subsequence of those
elements that satisfy certain conditions.
An iterable is an object that can be used repeatedly
in a subsequent loop statements. Eg: For loop
29. February 23, 2025 29
Looping in Lists
Python’s for and
in construct useful
when working
with lists, easy to
access each
element in a list.
30. February 23, 2025 30
Looping in Lists
Multiple ways to
access a List:
Iterator Function:
Loop over the
elements when
used with next()
method.
Uses built-in
iter()function
32. February 23, 2025 32
What is Functional Programming ?
Functional Programming
decomposes a problem into set of
functions
Map( ), filter( ), reduce( )
33. February 23, 2025 33
Filter Function
- Filter function constructs a list from those
elements of the list for which a function returns
True.
- filter(function, sequence)
- If sequence is a string, Unicode or a tuple, then the
result will be of same type otherwise it is always a
list.
34. February 23, 2025 34
Filter Function (Example)
def check(x):
if (x % 2 == 0 or x % 4 = =0):
return 1
events = list(filter(check, range(2, 22)))
print(events)
Output:
[ 2, 4, 6, 8, 10, 12, 14, 16, 18, 20]
Explanation:
The filter function returns True or False. Functions that returns a
Boolean value called as Predicates. Only those values divisible
by 2 or 4 is included in newly created list.
35. February 23, 2025 35
Map Function
- Map() function applies a particular function to
every element of a list. Its syntax is same as the
filter function.
- map(function, sequence)
- After applying the specified function on the
sequence the map() function returns the modified
list.
- It calls function(item)for each item in a sequence
a returns a list of return values.
36. February 23, 2025 36
Map Function (Example)
Explanation: Map() calls add_2() which adds 2 to every
element in the list
37. February 23, 2025 37
Map Function (Example)
def add(x,y):
return x+y
List1 = [1,2,3,4,5]
List2 = [6,7,8,9,10]
List3 = list(map(add, list1, list2))
Print(“sum of” , list1, “ and”, list2, “ =“, list3)
Output:
sum of [1,2,3,4,5] and [6,7,8,9,10] = [7,9,11,13,15]
Explanation:
Here more than one sequence is passed in map().
- Function must have as many as arguments as there are sequences.
- Each argument is called with corresponding item from each
sequence
38. February 23, 2025 38
Reduce Function
- Reduce() function returns a single value
generated by calling the function.
- reduce(function, sequence)