How to manipulate list in python
![How to manipulate list in python How to manipulate list in python](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj1XqkhEYEwsY_SrW9qGas-Z_8EyHj4WsZ_QoJPAjLvJ-kBEHwniKyPrtu-GM5Owycfx9XC09_P-l8r5PEpoe6GpdMaEKFOyQwm_z5vkSyrd5tWyceyrCOkzPI92HeL-UiSKw53fZbCnn7tBnr838E5-e_FqepyLS_KclFvl-VawxWUwVCILa4E-T89/s16000/Python%20List.png)
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