SlideShare a Scribd company logo
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
Python Data Structures
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
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]
List (Examples)
list1=[1,2,3,4,5] Output:
print(list1)  [1,2,3,4,5]
list2 =[‘A’, ‘B’, ‘C’, ‘d’, ‘e’] Output:
print(list2)  [‘A’, ‘B’, ‘C’, ‘d’,
‘e’]
list3=[“Apple”, “Banana”] Output:
print(list3)  [‘Apple’, ‘Banana’]
list4 = [1, ‘a’, “Dog”] Output:
print(list4)  [1, ‘a’, ‘Dog’]
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
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]
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.
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: [ ]
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]
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
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
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
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
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]
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”]
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]
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]
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]
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)
February 23, 2025 21
Using Lists as Stacks
- Stacks in computer science is used in function
calls.
February 23, 2025 22
Using Lists as Stacks
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
February 23, 2025 24
Using Lists as Stacks
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)
February 23, 2025 26
Using Lists as Queues
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
February 23, 2025 28
List Comprehensions
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.
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
February 23, 2025 31
February 23, 2025 32
What is Functional Programming ?
Functional Programming
decomposes a problem into set of
functions
Map( ), filter( ), reduce( )
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.
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.
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.
February 23, 2025 36
Map Function (Example)
Explanation: Map() calls add_2() which adds 2 to every
element in the list
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
February 23, 2025 38
Reduce Function
- Reduce() function returns a single value
generated by calling the function.
- reduce(function, sequence)
February 23, 2025 39
Reduce Function (Example)

More Related Content

Similar to Python _dataStructures_ List, Tuples, its functions (20)

PPTX
Unit 4.pptx python list tuples dictionary
shakthi10
 
PPTX
Module-2.pptx
GaganRaj28
 
PPTX
Python for Beginners(v3)
Panimalar Engineering College
 
PPTX
Introduction To Programming with Python-4
Syed Farjad Zia Zaidi
 
PDF
Data type list_methods_in_python
deepalishinkar1
 
PDF
Lists and its functions in python for beginners
Mohammad Usman
 
PPTX
UNIT-3 python and data structure alo.pptx
harikahhy
 
PDF
‘How to develop Pythonic coding rather than Python coding – Logic Perspective’
S.Mohideen Badhusha
 
PPTX
Pythonlearn-08-Lists for fundatmentals of Programming
ABIGAILJUDITHPETERPR
 
PDF
Unit 1-Part-4-Lists, tuples and dictionaries.pdf
Harsha Patil
 
PPTX
lists_list_of_liststuples_of_python.pptx
ShanthiJeyabal
 
PPTX
Python programming
sirikeshava
 
PDF
Py4Inf-08-Lists ListsListsListsListsListsListsListsListsListsListsListsLists.pdf
horiamommand
 
PDF
Python elements list you can study .pdf
AUNGHTET61
 
PDF
Module III.pdf
R.K.College of engg & Tech
 
DOCX
List Data Structure.docx
manohar25689
 
PPTX
fundamental of python --- vivek singh shekawat
shekhawatasshp
 
Unit 4.pptx python list tuples dictionary
shakthi10
 
Module-2.pptx
GaganRaj28
 
Python for Beginners(v3)
Panimalar Engineering College
 
Introduction To Programming with Python-4
Syed Farjad Zia Zaidi
 
Data type list_methods_in_python
deepalishinkar1
 
Lists and its functions in python for beginners
Mohammad Usman
 
UNIT-3 python and data structure alo.pptx
harikahhy
 
‘How to develop Pythonic coding rather than Python coding – Logic Perspective’
S.Mohideen Badhusha
 
Pythonlearn-08-Lists for fundatmentals of Programming
ABIGAILJUDITHPETERPR
 
Unit 1-Part-4-Lists, tuples and dictionaries.pdf
Harsha Patil
 
lists_list_of_liststuples_of_python.pptx
ShanthiJeyabal
 
Python programming
sirikeshava
 
Py4Inf-08-Lists ListsListsListsListsListsListsListsListsListsListsListsLists.pdf
horiamommand
 
Python elements list you can study .pdf
AUNGHTET61
 
List Data Structure.docx
manohar25689
 
fundamental of python --- vivek singh shekawat
shekhawatasshp
 

More from VidhyaB10 (12)

PPT
Preprocessing - Data Integration Tuple Duplication
VidhyaB10
 
PPT
Major Tasks in Data Preprocessing - Data cleaning
VidhyaB10
 
PPT
Applications ,Issues & Technology in Data mining -
VidhyaB10
 
PPTX
Python Visualization API Primersubplots
VidhyaB10
 
PPTX
Python_Functions_Modules_ User define Functions-
VidhyaB10
 
PPT
Datamining - Introduction - Knowledge Discovery in Databases
VidhyaB10
 
PPTX
INSTRUCTION PROCESSOR DESIGN Computer system architecture
VidhyaB10
 
PPTX
Disk Scheduling in OS computer deals with multiple processes over a period of...
VidhyaB10
 
PPTX
Unit 2 digital fundamentals boolean func.pptx
VidhyaB10
 
PPTX
Digital Fundamental - Binary Codes-Logic Gates
VidhyaB10
 
PPTX
unit 5-files.pptx
VidhyaB10
 
PPTX
Python_Unit1_Introduction.pptx
VidhyaB10
 
Preprocessing - Data Integration Tuple Duplication
VidhyaB10
 
Major Tasks in Data Preprocessing - Data cleaning
VidhyaB10
 
Applications ,Issues & Technology in Data mining -
VidhyaB10
 
Python Visualization API Primersubplots
VidhyaB10
 
Python_Functions_Modules_ User define Functions-
VidhyaB10
 
Datamining - Introduction - Knowledge Discovery in Databases
VidhyaB10
 
INSTRUCTION PROCESSOR DESIGN Computer system architecture
VidhyaB10
 
Disk Scheduling in OS computer deals with multiple processes over a period of...
VidhyaB10
 
Unit 2 digital fundamentals boolean func.pptx
VidhyaB10
 
Digital Fundamental - Binary Codes-Logic Gates
VidhyaB10
 
unit 5-files.pptx
VidhyaB10
 
Python_Unit1_Introduction.pptx
VidhyaB10
 
Ad

Recently uploaded (20)

PDF
Geographical Diversity of India 100 Mcq.pdf/ 7th class new ncert /Social/Samy...
Sandeep Swamy
 
PPTX
EDUCATIONAL MEDIA/ TEACHING AUDIO VISUAL AIDS
Sonali Gupta
 
PPTX
STAFF DEVELOPMENT AND WELFARE: MANAGEMENT
PRADEEP ABOTHU
 
PPTX
HUMAN RESOURCE MANAGEMENT: RECRUITMENT, SELECTION, PLACEMENT, DEPLOYMENT, TRA...
PRADEEP ABOTHU
 
PDF
Exploring the Different Types of Experimental Research
Thelma Villaflores
 
PDF
Stokey: A Jewish Village by Rachel Kolsky
History of Stoke Newington
 
PDF
Geographical diversity of India short notes by sandeep swamy
Sandeep Swamy
 
PPTX
PATIENT ASSIGNMENTS AND NURSING CARE RESPONSIBILITIES.pptx
PRADEEP ABOTHU
 
PDF
Mahidol_Change_Agent_Note_2025-06-27-29_MUSEF
Tassanee Lerksuthirat
 
PPTX
care of patient with elimination needs.pptx
Rekhanjali Gupta
 
PPTX
Post Dated Cheque(PDC) Management in Odoo 18
Celine George
 
PDF
Horarios de distribución de agua en julio
pegazohn1978
 
PDF
Chapter-V-DED-Entrepreneurship: Institutions Facilitating Entrepreneurship
Dayanand Huded
 
PDF
Aprendendo Arquitetura Framework Salesforce - Dia 03
Mauricio Alexandre Silva
 
PPTX
How to Configure Re-Ordering From Portal in Odoo 18 Website
Celine George
 
PDF
The Different Types of Non-Experimental Research
Thelma Villaflores
 
PPT
Talk on Critical Theory, Part II, Philosophy of Social Sciences
Soraj Hongladarom
 
PDF
Characteristics, Strengths and Weaknesses of Quantitative Research.pdf
Thelma Villaflores
 
PPTX
How to Create Odoo JS Dialog_Popup in Odoo 18
Celine George
 
PPTX
How to Set Up Tags in Odoo 18 - Odoo Slides
Celine George
 
Geographical Diversity of India 100 Mcq.pdf/ 7th class new ncert /Social/Samy...
Sandeep Swamy
 
EDUCATIONAL MEDIA/ TEACHING AUDIO VISUAL AIDS
Sonali Gupta
 
STAFF DEVELOPMENT AND WELFARE: MANAGEMENT
PRADEEP ABOTHU
 
HUMAN RESOURCE MANAGEMENT: RECRUITMENT, SELECTION, PLACEMENT, DEPLOYMENT, TRA...
PRADEEP ABOTHU
 
Exploring the Different Types of Experimental Research
Thelma Villaflores
 
Stokey: A Jewish Village by Rachel Kolsky
History of Stoke Newington
 
Geographical diversity of India short notes by sandeep swamy
Sandeep Swamy
 
PATIENT ASSIGNMENTS AND NURSING CARE RESPONSIBILITIES.pptx
PRADEEP ABOTHU
 
Mahidol_Change_Agent_Note_2025-06-27-29_MUSEF
Tassanee Lerksuthirat
 
care of patient with elimination needs.pptx
Rekhanjali Gupta
 
Post Dated Cheque(PDC) Management in Odoo 18
Celine George
 
Horarios de distribución de agua en julio
pegazohn1978
 
Chapter-V-DED-Entrepreneurship: Institutions Facilitating Entrepreneurship
Dayanand Huded
 
Aprendendo Arquitetura Framework Salesforce - Dia 03
Mauricio Alexandre Silva
 
How to Configure Re-Ordering From Portal in Odoo 18 Website
Celine George
 
The Different Types of Non-Experimental Research
Thelma Villaflores
 
Talk on Critical Theory, Part II, Philosophy of Social Sciences
Soraj Hongladarom
 
Characteristics, Strengths and Weaknesses of Quantitative Research.pdf
Thelma Villaflores
 
How to Create Odoo JS Dialog_Popup in Odoo 18
Celine George
 
How to Set Up Tags in Odoo 18 - Odoo Slides
Celine 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]
  • 5. List (Examples) list1=[1,2,3,4,5] Output: print(list1)  [1,2,3,4,5] list2 =[‘A’, ‘B’, ‘C’, ‘d’, ‘e’] Output: print(list2)  [‘A’, ‘B’, ‘C’, ‘d’, ‘e’] list3=[“Apple”, “Banana”] Output: print(list3)  [‘Apple’, ‘Banana’] list4 = [1, ‘a’, “Dog”] Output: print(list4)  [1, ‘a’, ‘Dog’]
  • 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.
  • 22. February 23, 2025 22 Using Lists as Stacks
  • 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
  • 24. February 23, 2025 24 Using Lists as Stacks
  • 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)
  • 26. February 23, 2025 26 Using Lists as Queues
  • 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
  • 28. February 23, 2025 28 List Comprehensions
  • 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)
  • 39. February 23, 2025 39 Reduce Function (Example)