This document discusses various methods for modifying lists in Python, including:
- Directly modifying elements using indexes to assign new values
- Using built-in functions like append() to add elements to the end of a list, insert() to add at a specific position, remove() to remove the first matching element, and pop() to remove and return an element at an index.
- Examples are provided for modifying top-level and sub-level elements as well as removing entire levels from nested lists.
Download as PPTX, PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
22 views
Lect2 Lists 2
This document discusses various methods for modifying lists in Python, including:
- Directly modifying elements using indexes to assign new values
- Using built-in functions like append() to add elements to the end of a list, insert() to add at a specific position, remove() to remove the first matching element, and pop() to remove and return an element at an index.
- Examples are provided for modifying top-level and sub-level elements as well as removing entire levels from nested lists.
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 20
Lists Part -2
In this lecture Modify lists ◦ Add elements ◦ Remove elements
Python for Data Science 2
Modifying components of a list Elements inside a list can be modified using two methods Assigning the new element directly to the index position that has to be updated Using in built functions where the element that is to be updated with is given as an input to the function along with the index position
Python for Data Science 3
Modifying components of a list using index Assign the values to be changed to corresponding index of the list Eg- Change the value in top level components of a list
Existing list
Python for Data Science 4
Modifying components of a list using index Here the value of 4 should be updated to 5
Print the updated list
Python for Data Science 5
Modifying components of a list using index Eg- Change value in sub level components of a list
John has been replaced with Karan
Python for Data Science 6
Modifying components using append() append()- adds an object at the end of the list Syntax: list_name[index].append(object)
In the above syntax if the ‘index’ is not specified, then the
object gets added as a new level in the existing list There are two ways to add an object to a list:- ◦ Adding an element to a list ◦ Adding a list to a list
Python for Data Science 7
Modifying components using append() Adding an element to a list Adding number ‘5’ to the level id in employee_list
Adding name ‘nirmal’ to the level employee_name
in employee_list
Print the updated list
Python for Data Science 8
Modifying components using append() Adding a list to a list (also termed as concatenation of lists) Adding a new list age to the existing employee_list
The new list gets added as a new level at the end
Print the updated list
Python for Data Science 9
Modifying components using insert() i n s e r t ( ) - adds an object at the given position in a list Syntax: list_name[index].insert(position,object)
Existing list
Addingnumber ‘6’ at the 1st position to the level id from
employee_list
Python for Data Science 10
Modifying components using insert() Print the updated list
Python for Data Science 11
Modifying components using del del- removes the object at the specified index number Syntax: del list_name[index1][index2]
In the above syntax,
◦ index1- index number of the top level of components to be dropped ◦ index2 corresponds to the sub level of components to be dropped
Python for Data Science 12
Modifying components using del Existing list
Dropthe last level i.e. age from
employee_list
Print the updated list
Python for Data Science 13
Modifying components using remove() remove()- removes the first matching object from a list Syntax: list_name[index].remove(object)
Existing list
Python for Data Science 14
Modifying components using remove() Remove ‘Ram’ from the level employee_name fro employee_list m
Print updated list
Here ‘Ram’ occurs only
once Python for Data Science 15 Modifying components using remove() Consider another list
Removing the first occurrence of
‘Low’
Print the updated list
Python for Data Science 16
Modifying components using pop() pop()- displays the object that is being removed from the list at the specified index number Syntax: list_name[index1].pop(index2)
In the above syntax,
◦ index1- index number of the top level of components to be dropped ◦ index2 corresponds to the sub level of components to be dropped
Python for Data Science 17
Modifying components using pop() Existing list
Removing number ‘4’ from the 5th position of level id
from employee_list
Print the updated list
Python for Data Science 18
Summar y Manipulate lists directly using the index number Manipulate lists using functions: ◦ inser append - adds- addsan element at theatspecified an element position the end of the tlist - removes the element at the specified ◦ remove del position the first matching element - removes ◦ pop - displays and removes the element at the specified position
Fire safety for very tall buildings : engineering guide Second Edition Society Of Fire Protection Engineers - Quickly download the ebook to read anytime, anywhere
Fire safety for very tall buildings : engineering guide Second Edition Society Of Fire Protection Engineers - Quickly download the ebook to read anytime, anywhere