THE DELIVERY MAN
The Delivery Man – Python List Operations
Introduction
Lists in Python are used to store multiple items in a single variable. They are flexible, allowing us to add, remove, update, and manipulate elements easily. In this task, different list operations are performed to simulate a delivery man's workflow.
1. Creating a list and accessing an item
items = ["Notebook", "Pencil", "Eraser", "Ruler", "Marker"]
print(items[2])
Approach: Indexing
Why: Lists use zero-based indexing, so the third item is accessed using index 2.
2. Adding a new item
items.append("Glue Stick")
print(items)
Approach: append() method
Why: It adds an item at the end of the list, which is ideal for adding new deliveries.
3. Inserting an item
items.insert(2, "Highlighter")
print(items)
Approach: insert() method
Why: Used when we need to add an item at a specific position instead of the end.
4. Removing an item
items.remove("Ruler")
print(items)
Approach: remove() method
Why: Removes a specific value directly.
Alternative: pop() removes by index, but here we know the item name.
5. Printing a sublist
print(items[:3])
Approach: List slicing
Why: Efficient way to extract a portion of the list without modifying the original list.
6. Converting to uppercase
upper_items = [item.upper() for item in items]
print(upper_items)
Approach: List comprehension
Why: More concise and readable compared to using loops.
7. Checking if an item exists
if "Marker" in items:
print("Marker is in the list")
else:
print("Marker is not in the list")
Approach: Membership operator (in)
Why: Simple and efficient way to check existence.
8. Counting items
print(len(items))
Approach: len() function
Why: Gives total number of elements in the list.
9. Sorting the list
items.sort()
print(items)
Approach: sort() method
Why: Sorts the list in place, making it efficient.
10. Reversing the list
items.reverse()
print(items)
Approach: reverse() method
Why: Directly reverses the order of elements.
11. Nested list (item with time)
delivery = [["Notebook", "10AM"], ["Pencil", "11AM"]]
print(delivery[0])
Approach: Nested list
Why: Allows grouping related data together (item + time).
12. Counting occurrences
print(items.count("Ruler"))
Approach: count() method
Why: Counts how many times a value appears.
13. Finding index
print(items.index("Pencil"))
Approach: index() method
Why: Finds the position of an element in the list.
14. Extending the list
new_items = ["Pen", "Sharpener"]
items.extend(new_items)
print(items)
Approach: extend() method
Why: Adds multiple items at once.
15. Clearing the list
items.clear()
print(items)
Approach: clear() method
Why: Removes all elements efficiently.
16. Repeating items
items = ["Notebook"] * 3
print(items)
Approach: List multiplication
Why: Quick way to repeat elements.
17. Nested list comprehension
items = ["Notebook", "Pencil", "Eraser"]
result = [[item, len(item)] for item in items]
print(result)
Approach: Nested list comprehension
Why: Combines item and its length in a clean and compact way.
18. Filtering items
filtered = [item for item in items if "e" in item]
print(filtered)
Approach: Conditional list comprehension
Why: Efficient way to filter elements.
19. Removing duplicates
items = ["Notebook", "Pencil", "Notebook"]
unique_items = list(set(items))
print(unique_items)
Approach: Using set()
Why: Automatically removes duplicates.
Limitation: Order is not preserved.
Conclusion
This task demonstrates various list operations in Python such as adding, removing, sorting, and filtering elements. By using built-in methods and list comprehensions, we can write efficient and readable code. Choosing the right method depends on the situation, such as whether we need to maintain order or improve performance.
Comments
Post a Comment