How to manipulate list in python
We can manipulate list in python using function append(), insert(), and by index
Manipulate list in python
#1. append()
append() function is used to add a new element in the list at the last position.
country = [ ‘India’, ‘USA’, ‘Australia’, ‘Canada’ ]
print( country )
country.append(“UK”)
print( ‘ After adding a new country ‘ )
print( country )
Output
[ ‘India’, ‘USA’, ‘Australia’, ‘Canada’ ]
After adding a new country
[ ‘India’, ‘USA’, ‘Australia’, ‘Canada’, ‘UK’ ]
#2. insert()
insert() function is used to add a new element in list at given position.
color = [ ‘Green’, ‘Red’, ‘Black’, ‘Yellow’ ]
print( color )
color.insert(‘Blue’)
print( ‘ After adding a new color’ )
print( color)
Output
[ ‘Green’, ‘Red’, ‘Black’, ‘Yellow’ ]
[ ‘Green’, ‘Red’, ‘Black’, ‘Yellow’, ‘Blue’ ]
#3 By using index
Output
Important Links: Informatics Practices
What is Python List
Characteristics of Python List
How to Create Python List
How to Traverse a list in Python
How to Modify multiple elements in Python list
Slicing in Python List
Built in function of Python List