Insert method | Remove Data From List | Remove Item From Position or from index | Remove method | Delete statement | Looping in list In Python

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

 

See my more website

Pramod Kumar Yadav is from Janakpur Dham, Nepal. He was born on December 23, 1994, and has one elder brother and two elder sisters. He completed his education at various schools and colleges in Nepal and completed a degree in Computer Science Engineering from MITS in Andhra Pradesh, India. Pramod has worked as the owner of RC Educational Foundation Pvt Ltd, a teacher, and an Educational Consultant, and is currently working as an Engineer and Digital Marketer.



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”

Leave a Comment