DEV Community

Santhoshi Mary A
Santhoshi Mary A

Posted on

Task – The Delivery MAN – Python List

  1. Create a list of five delivery items and print the third item items = ["Notebook", "Pencil", "Eraser", "Ruler", "Marker"] print("Third item:", items[2])

Explanation:

Lists are indexed starting from 0.
The third item is at index 2.

  1. Add “Glue Stick” to the end of the list items.append("Glue Stick") print("Updated list:", items)

Explanation:

append() adds an element to the end of the list.

  1. Insert “Highlighter” between the second and third items items.insert(2, "Highlighter") print("After inserting Highlighter:", items)

Explanation:

insert(index, value) adds an item at a specific position.
Index 2 places it between the second and third items.

  1. Remove “Ruler” from the list items.remove("Ruler") print("After removing Ruler:", items)

Explanation:

remove() deletes a specific value from the list.

  1. Print only the first three items print("First three items:", items[:3])

Explanation:

List slicing [:3] returns elements from index 0 to 2.

  1. Convert all items to uppercase using list comprehension uppercase_items = [item.upper() for item in items] print("Uppercase list:", uppercase_items)

Explanation:

List comprehension creates a new list.
upper() converts strings to uppercase.

  1. Check if “Marker” is in the list if "Marker" in items: print("Marker is found in the list.") else: print("Marker is not found in the list.")

Explanation:

in checks for membership in a list.

  1. Print the number of delivery items print("Number of items:", len(items))

Explanation:

len() returns the total number of elements.

  1. Sort the list alphabetically items.sort() print("Sorted list:", items)

Explanation:

sort() arranges items in alphabetical order.

  1. Reverse the list items.reverse() print("Reversed list:", items)

Explanation:

reverse() changes the order of elements.

  1. Create a list of items with delivery time delivery_details = [ ["Notebook", "10:00 AM"], ["Pencil", "10:15 AM"], ["Eraser", "10:30 AM"] ]

print("First item and time:", delivery_details[0])

Explanation:

This is a nested list (list inside a list).
delivery_details[0] accesses the first sublist.

  1. Count how many times “Ruler” appears count_ruler = items.count("Ruler") print("Ruler appears:", count_ruler, "times")

Explanation:

count() returns how many times a value appears.

  1. Find the index of “Pencil” index_pencil = items.index("Pencil") print("Index of Pencil:", index_pencil)

Explanation:

index() returns the position of the element.

  1. Extend the list with new items new_items = ["Sharpener", "Scale"] items.extend(new_items) print("Extended list:", items)

Explanation:

extend() adds multiple elements from another list.

  1. Clear all items from the list items.clear() print("List after clearing:", items)

Explanation:

clear() removes all elements from the list.

  1. Create a list with “Notebook” repeated three times repeated_list = ["Notebook"] * 3 print("Repeated list:", repeated_list)

Explanation:

  • repeats elements in a list.
  • Create a list with each item and its length items = ["Notebook", "Pencil", "Eraser"]

item_lengths = [[item, len(item)] for item in items]
print("Item and its length:", item_lengths)

Explanation:

List comprehension creates sublists.
len(item) calculates string length.

  1. Filter items containing the letter “e” filtered_items = [item for item in items if "e" in item.lower()] print("Items containing 'e':", filtered_items)

Explanation:

Conditional list comprehension filters items.
lower() ensures case-insensitive checking.

  1. Remove duplicate items items = ["Notebook", "Pencil", "Notebook", "Eraser"]

unique_items = list(set(items))
print("Unique items:", unique_items)

Explanation:

set() removes duplicates.
Convert back to list using list().

Top comments (0)