Insert Method Python
Python list method insert() inserts object obj into the insert list.
Syntax:
Following is the syntax to insert() method –
listname.list(index, obj)
Parameter Index – This is the index where the object needs to be inserted.
obj – This is the item to be inserted in the given list return value. This method does not return any value, but it inserts the given element into the given index.
Example:
mixed=[]
mixed.insert(0,'zero')
print(mixed)
Output:
C:\Users\Pramod\PycharmProjects\pythonProject\venv\Scripts\python.exe C:/Users/Pramod/PycharmProjects/pythonProject/main.py
['zero']
Process finished with exit code 0
How do I Remove Data From List?
The remove() method removes the object passed as an argument. If you need to remove elements based on the index (such as the fourth element or the last element), you can use the pop() method. In addition, you can use the Dell statement to remove items from a list or to delete an entire list.
Syntax:
mixed.pop()
Example:
mixed = [1, 2, 3, [4, 5, 6], 'seven', 8.0]
popped=mixed.pop()
print('popped item is:',popped)
print('Remain Item in List',mixed)
Output:
C:\Users\Pramod\PycharmProjects\pythonProject\venv\Scripts\python.exe C:/Users/Pramod/PycharmProjects/pythonProject/main.py
popped item is: 8.0
Remain Item in List [1, 2, 3, [4, 5, 6], 'seven']
Process finished with exit code 0
Remove Item From Position or From The Index
In this program, we will remove the item from the list from a particular position or index of the list.
Syntax:
list.pop(index_number)
Example:
mixed = [1, 2, 3, [4, 5, 6], 'seven', 8.0]
popped=mixed.pop(3)
print('popped item is:',popped)
print('Remain Item in List',mixed)
Output:
C:\Users\Pramod\PycharmProjects\pythonProject\venv\Scripts\python.exe C:/Users/Pramod/PycharmProjects/pythonProject/main.py
popped item is: [4, 5, 6]
Remain Item in List [1, 2, 3, 'seven', 8.0]
Process finished with exit code 0
Remove method:
Remove() method is used to remove the particular element from the list and the element will delete permanently. When you try to print the removed element then it will display none. It will take elements as an argument.
Syntax:
list.remove(element)
Example:
mixed = [1, 2, 3, [4, 5, 6], 'seven', 8.0]
popped=mixed.remove('seven')
print('popped item is:',popped)
print('Remain Item in List',mixed)
Output:
C:\Users\Pramod\PycharmProjects\pythonProject\venv\Scripts\python.exe C:/Users/Pramod/PycharmProjects/pythonProject/main.py
popped item is: None
Remain Item in List [1, 2, 3, [4, 5, 6], 8.0]
Process finished with exit code 0
Delete statement in Python List:
# delete method is used to delete the element present in the list. It will take the index number as an argument to delete the item. To delete items from the list, we use the del keyword.
Syntax:
del list_name[index]
Example:
mixed = [1, 2, 3, [4, 5, 6], 'seven', 8.0]
del mixed[3]
print('Remain Item in List',mixed)
Output:
C:\Users\Pramod\PycharmProjects\pythonProject\venv\Scripts\python.exe C:/Users/Pramod/PycharmProjects/pythonProject/main.py
Remain Item in List [1, 2, 3, 'seven', 8.0]
Process finished with exit code 0
Looping Through List Python:
In this program, we will learn to use a loop to find the element from the list or to print the element available in the list.
Syntax:
for i in list_name:
Statement
Example:
mixed = [1, 2, 3, [4, 5, 6], 'seven', 8.0]
for i in mixed:
print(i)
Output:
C:\Users\Pramod\PycharmProjects\pythonProject\venv\Scripts\python.exe C:/Users/Pramod/PycharmProjects/pythonProject/main.py
1
2
3
[4, 5, 6]
seven
8.0
Process finished with exit code 0
- Python Hello World Program
- Python Comment | Creating a Comment | multiline comment | example
- Python Dictionary Introduction | Why to use dictionary | with example
- How to do Sum or Addition in python
- Python Reverse number
- find a common number in python
- addition of number using for loop and providing user input data in python
- Python Count char in String
- Python Last Character from String
- Python Odd and Even | if condition
- Python Greater or equal number
- Python PALINDROME NUMBER
- Python FIBONACCI SERIES
- Python Dictionary | Items method | Definition and usage
- Python Dictionary | Add data , POP , popitem , update method
- Python Dictionary | update() method
- Delete statement , Looping in list In Python
- Odd and Even using Append in python
- Python | Simple Odd and Even number
See my more website
1 thought on “Insert method | Remove Data From List | Remove Item From Position or from index | Remove method | Delete statement | Looping in list In Python”