Python List Operations: Sorting, Traversing, Deleting, Adding, Modifying, Indexing, and Basic List Functions
This tutorial demonstrates essential Python list operations, covering case‑sensitive and case‑insensitive sorting, reverse ordering, iteration with for and while loops, element removal by value or index, clearing, appending, inserting, extending, item modification, slicing, indexing, length checking, and mixed‑type lists, all illustrated with clear code examples.
1. Sorting
Shows case‑sensitive sorting, case‑insensitive sorting using key=str.lower , and reverse sorting with reverse=True .
fruits = ["banana", "Apple", "cherry", "orange"]
sorted_fruits = sorted(fruits)
print(sorted_fruits) # ['Apple', 'banana', 'cherry', 'orange']
sorted_fruits = sorted(fruits, key=str.lower)
print(sorted_fruits) # ['Apple', 'banana', 'cherry', 'orange']
sorted_fruits = sorted(fruits, key=str.lower, reverse=True)
print(sorted_fruits) # ['orange', 'cherry', 'banana', 'Apple']2. Traversing Lists
Demonstrates iteration using a for loop and a while loop.
fruits = ["banana", "Apple", "cherry", "orange"]
for fruit in fruits:
print(fruit)
index = 0
while index < len(fruits):
print(fruits[index])
index += 13. Deleting List Items
Illustrates removal by value with remove() , deletion by index with del , and clearing the entire list with clear() .
fruits.remove("Apple") # removes first matching element
print(fruits) # ['banana', 'cherry', 'orange']
del fruits[1] # deletes element at index 1
print(fruits) # ['banana', 'cherry', 'orange']
fruits.clear()
print(fruits) # []4. Adding List Items
Shows how to append at the end, insert at a specific position, and extend with another list.
fruits.append("grape")
print(fruits) # ['banana', 'Apple', 'cherry', 'orange', 'grape']
fruits.insert(1, "grape")
print(fruits) # ['banana', 'grape', 'Apple', 'cherry', 'orange']
more_fruits = ["grape", "kiwi"]
fruits.extend(more_fruits)
print(fruits) # ['banana', 'Apple', 'cherry', 'orange', 'grape', 'kiwi']5. Modifying List Items
Provides examples of changing a single element, updating a slice, and inserting multiple elements via slice assignment.
fruits[1] = "apple"
print(fruits) # ['banana', 'apple', 'cherry', 'orange']
fruits[1:3] = ["apple", "berry"]
print(fruits) # ['banana', 'apple', 'berry', 'orange']
fruits[1:1] = ["apple", "berry"]
print(fruits) # ['banana', 'apple', 'berry', 'Apple', 'cherry', 'orange']6. Indexing and Slicing
Demonstrates normal indexing, negative indexing, slice ranges, negative slice ranges, and membership testing.
print(fruits[1]) # Apple
print(fruits[-1]) # orange
print(fruits[1:3]) # ['Apple', 'cherry']
print(fruits[-3:-1]) # ['Apple', 'cherry']
if "Apple" in fruits:
print("Apple 存在于列表中")
else:
print("Apple 不存在于列表中")7. Basic List Operations
Creates a list, prints it, shows how to obtain its length, and demonstrates a mixed‑type list.
fruits = ["banana", "Apple", "cherry", "orange"]
print(fruits) # ['banana', 'Apple', 'cherry', 'orange']
print(len(fruits)) # 4
mixed_list = [1, "banana", 3.14, True]
print(mixed_list) # [1, 'banana', 3.14, True]Test Development Learning Exchange
Test Development Learning Exchange
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.