Common Python List Operations: Sorting, Filtering, Searching, Merging, and More
This article demonstrates ten practical Python list techniques—including sorting by ID, filtering by condition, locating elements, removing items, merging lists, finding intersections and differences, converting to dictionaries, using list comprehensions, and deduplicating—each illustrated with clear code examples and expected output.
1. Sort a list by ID – Given an API that returns a list of objects, the list can be ordered by the id field using sorted with a lambda key.
data = [
{"id": 3, "name": "Charlie"},
{"id": 1, "name": "Alice"},
{"id": 2, "name": "Bob"}
]
sorted_data = sorted(data, key=lambda x: x['id'])
print(sorted_data) [
{"id": 1, "name": "Alice"},
{"id": 2, "name": "Bob"},
{"id": 3, "name": "Charlie"}
]2. Filter elements in a list – To keep only objects that satisfy a condition (e.g., age > 18 ), use filter with a lambda.
users = [
{"name": "Alice", "age": 22},
{"name": "Bob", "age": 17},
{"name": "Charlie", "age": 25}
]
adults = list(filter(lambda user: user['age'] > 18, users))
print(adults) [{"name": "Alice", "age": 22}, {"name": "Charlie", "age": 25}]3. Find an element in a list – Use next with a generator expression to retrieve the first matching object.
users = [
{"name": "Alice", "age": 22},
{"name": "Bob", "age": 17},
{"name": "Charlie", "age": 25}
]
alice = next((user for user in users if user['name'] == 'Alice'), None)
print(alice) {"name": "Alice", "age": 22}4. Delete elements from a list – Re‑assign the list with a comprehension that keeps only items meeting the condition.
users = [
{"name": "Alice", "age": 22},
{"name": "Bob", "age": 17},
{"name": "Charlie", "age": 25}
]
users = [user for user in users if user['age'] > 18]
print(users) [{"name": "Alice", "age": 22}, {"name": "Charlie", "age": 25}]5. Merge two lists – Concatenate two lists with the + operator.
list1 = ["apple", "banana"]
list2 = ["orange", "grape"]
merged_list = list1 + list2
print(merged_list) ["apple", "banana", "orange", "grape"]6. Find the intersection of two lists – Convert both lists to set objects and use the & operator.
list1 = ["apple", "banana", "orange"]
list2 = ["orange", "grape", "banana"]
common_elements = list(set(list1) & set(list2))
print(common_elements) ["banana", "orange"]7. Find the difference of two lists – Use set subtraction to obtain items present in the first list but not the second.
list1 = ["apple", "banana", "orange"]
list2 = ["orange", "grape", "banana"]
difference = list(set(list1) - set(list2))
print(difference) ["apple"]8. Convert a list to a dictionary – Pair two parallel lists of keys and values with zip and feed them to dict .
keys = ["name", "age"]
values = ["Alice", 22]
dictionary = dict(zip(keys, values))
print(dictionary) {"name": "Alice", "age": 22}9. Use a list comprehension for filtering – A concise one‑liner that builds a new list of objects meeting a condition.
users = [
{"name": "Alice", "age": 22},
{"name": "Bob", "age": 17},
{"name": "Charlie", "age": 25}
]
adults = [user for user in users if user['age'] > 18]
print(adults) [{"name": "Alice", "age": 22}, {"name": "Charlie", "age": 25}]10. Remove duplicate items from a list – Convert the list to a set and back to a list to obtain unique elements.
items = ["apple", "banana", "orange", "apple", "grape", "banana"]
unique_items = list(set(items))
print(unique_items) ["apple", "banana", "orange", "grape"]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.