B.Tech Students: Apply for Live Programming Internship C, C++, Java, Python ,Web page Designing, PHP Remove Items from List in Python | LiveCodeProgramming
Python Installation First Python Program Control Flow if...else nested if else match-case logical-operators for loops while loops break / continue Functions Defining Functions Function Arguments Function return value Lambda Function Recursion Exception Handling Exception Handling Try Except Else and Finally Raise Exception Custom Exceptions File Handling File in python Read File Write File Append File Delete File Exception Handling

Removing Items from a List in Python

Python provides many ways to remove items from a list. Let's understand remove(), pop(), del, and clear() with examples.

Python में list से item हटाने के कई तरीके हैं। आइए remove(), pop(), del और clear() को उदाहरण के साथ समझें।

1. Using remove() - by value
students = ["Raj", "Simran", "Aman"]
students.remove("Simran")
print(students)
Output:
["Raj", "Aman"]
2. Using pop() - remove last item
students = ["Raj", "Simran", "Aman"]
students.pop()
print(students)
Output:
["Raj", "Simran"]
3. Using pop(index)
students = ["Raj", "Simran", "Aman"]
students.pop(0)
print(students)
Output:
["Simran", "Aman"]
4. Using del - by index
students = ["Raj", "Simran", "Aman"]
del students[1]
print(students)
Output:
["Raj", "Aman"]
5. Using del - full list
students = ["Raj", "Simran", "Aman"]
del students
# Now students no longer exists
Output:
NameError: name 'students' is not defined
6. Using clear()
students = ["Raj", "Simran"]
students.clear()
print(students)
Output:
[]
7. Removing Dynamically (user input)
students = ["Raj", "Simran", "Aman"]
to_remove = input("Enter name to remove: ")
if to_remove in students:
    students.remove(to_remove)
else:
    print("Name not in list")
print(students)
Output:
Enter name to remove: Simran
['Raj', 'Aman']