Explore all the essential methods for manipulating Python lists. Learn how to add, remove, sort, and search elements with clear examples..
append(item)
– Adds an item to the endinsert(index, item)
– Adds item at specific indexremove(item)
– Removes the first matching itempop(index)
– Removes item at given index (default is last)sort()
- Sort the listYou can add elements to a list using methods like append()
or insert()
.
fruits = ['Apple', 'Banana', 'Mango', 'Orange']
# Append an element to the end of the list
fruits.append("Grape")
print(fruits) # Output: ['Apple', 'Banana', 'Mango', 'Orange', 'Grape']
# Insert an element at a specific position
fruits.insert(1, "Orange")
print(fruits) # Output: ['Apple', 'Orange', 'Banana', 'Mango', 'Orange', 'Grape']
Elements can be removed from a list using methods like remove()
, pop()
, or del
.
# Remove a specific element by value
fruits.remove("Banana")
print(fruits) # Output: ['Apple', 'Orange', 'Mango', 'Orange', 'Grape']
# Remove last element by using pop
fruits.pop() # Output: ['Apple', 'Orange', 'Mango', 'Orange']
# Remove an element by index using pop
fruits.pop(1)
print(fruits) # Output: ['Apple', 'Mango', 'Orange']
This video covers:
append
[00:22] and insert
[00:35] methods.append
and insert
[00:52].remove
[01:24] and pop
[01:45] methods.pop
removes and returns elements [01:50], including the last item when no index is specified [02:34].This video covers:
pop
method removes the last item in a list by default and displays it [00:38].pop
method [00:46].You can sort elements in a list using the sort()
method.
numbers = [3, 1, 4, 1, 5, 9, 2, 6, 5]
fruits = ['Apple', 'banana', 'Mango', 'orange']
# Sort a list in ascending order (modifies the original list)
numbers.sort()
print(numbers) # Output: [1, 1, 2, 3, 4, 5, 5, 6, 9]
# Sort a list in descending order
numbers.sort(reverse=True)
print(numbers) # Output: [9, 6, 5, 5, 4, 3, 2, 1, 1]
# Case-sensitive sort (ASCII order: uppercase comes before lowercase)
fruits.sort()
print(fruits) # Output: ['Apple', 'Mango', 'banana', 'orange']
For more details on sorting, see Sorting Techniques in Python - Sorting How To
append(item)
– Adding a new task to a to-do listtodo_list = ["Buy groceries", "Finish report"]
todo_list.append("Call mom")
print(todo_list) # Output: ["Buy groceries", "Finish report", "Call mom"]
insert(index, item)
– Adding an emergency task at a specific positiontodo_list = ["Buy groceries", "Finish report"]
todo_list.insert(0, "Attend emergency meeting")
print(todo_list) # Output: ["Attend emergency meeting", "Buy groceries", "Finish report"]
remove(item)
– Removing a completed tasktodo_list = ["Buy groceries", "Finish report", "Call mom"]
todo_list.remove("Buy groceries")
print(todo_list) # Output: ["Finish report", "Call mom"]
pop(index)
– Removing the last task (or a specific one)todo_list = ["Finish report", "Call mom", "Pay bills"]
last_task = todo_list.pop() # Removes & returns "Pay bills"
print(todo_list) # Output: ["Finish report", "Call mom"]
# Removing a task at index 0
first_task = todo_list.pop(0) # Removes & returns "Finish report"
print(todo_list) # Output: ["Call mom"]
sort()
– Sorting a list of names alphabeticallyattendees = ["Zara", "Alice", "Bob", "Eve"]
attendees.sort()
print(attendees) # Output: ["Alice", "Bob", "Eve", "Zara"]
Write a Python program that:
append()
.insert()
.pop()
.Example Output:
Initial List: ['Python', 'Java', 'C++']
After Append: ['Python', 'Java', 'C++', 'JavaScript']
After Insert: ['Python', 'Ruby', 'Java', 'C++', 'JavaScript']
After Pop: ['Python', 'Ruby', 'Java', 'C++']
Write a Python program that:
Example Input:
Enter a city: Paris
Enter a city: London
Enter a city: Tokyo
Example Output:
Cities: ['Paris', 'London', 'Tokyo']
IndexError
when accessing out-of-range indices).len(list)
to check list length.Example Addition:
colors = ["red", "blue"]
try:
print(colors[2]) # Raises IndexError
except IndexError:
print("Index out of range!")