DEV Community

Haripriya V
Haripriya V

Posted on

Task 3 – The Delivery MAN – Python List

1.Create a list of five delivery items and print the third item in the list. eg: [“Notebook”, “Pencil”, “Eraser”, “Ruler”, “Marker”]

CODE:

items = ["Notebook", "Pencil", "Eraser", "Ruler", "Marker"]
print(items[2])

OUTPUT:

Eraser

EXPLANATION:

  • List indexing starts from 0

  • Third item → index 2

2.A new delivery item “Glue Stick” needs to be added to the list. Add it to the end of the list and print the updated list.

CODE:

items = ["Notebook", "Pencil", "Eraser", "Ruler", "Marker"]
items.append("Glue Stick")
print(items)

OUTPUT:

['Notebook', 'Pencil', 'Eraser', 'Ruler', 'Marker', 'Glue Stick']

EXPLANATION:

  • append() adds item to the end

3.Insert “Highlighter” between the second and third items and print the updated list.

CODE:


items = ["Notebook", "Pencil", "Eraser", "Ruler", "Marker", 'Glue Stick']
items.insert(2, "Highlighter")
print(items)

OUTPUT:

['Notebook', 'Pencil', 'Highlighter', 'Eraser', 'Ruler', 'Marker', 'Glue Stick']

EXPLANATION:

  • insert(index, value) adds at specific position

4.One delivery was canceled. Remove “Ruler” from the list and print the updated list.

CODE:

items=['Notebook', 'Pencil', 'Highlighter', 'Eraser', 'Ruler', 'Marker', 'Glue Stick']
items.remove("Ruler")
print(items)

OUTPUT:

['Notebook', 'Pencil', 'Highlighter', 'Eraser', 'Marker', 'Glue Stick']

EXPLANATION:

  • remove() deletes specific value

5.The delivery man needs to deliver only the first three items. Print a sublist containing only these items.

CODE:

items=['Notebook', 'Pencil', 'Highlighter', 'Eraser', 'Marker', 'Glue Stick']
print(items[:3])

OUTPUT:

['Notebook', 'Pencil', 'Highlighter']

EXPLANATION:

  • Slicing [:3] gets first 3 items

6.The delivery man has finished his deliveries. Convert all item names to uppercase using a list comprehension and print the new list.

CODE:

items=['Notebook', 'Pencil', 'Highlighter', 'Eraser', 'Marker', 'Glue Stick']
upper_items = [item.upper() for item in items]
print(upper_items)

OUTPUT:

['NOTEBOOK', 'PENCIL', 'HIGHLIGHTER', 'ERASER', 'MARKER', 'GLUE STICK']

EXPLANATION:

  • List comprehension transforms each item

7.Check if “Marker” is still in the list and print a message indicating whether it is found.

CODE:

items=['Notebook', 'Pencil', 'Highlighter', 'Eraser', 'Marker', 'Glue Stick']
if "Marker" in items:
print("Marker found")
else:
print("Marker not found")

OUTPUT:

Marker found

EXPLANATION:

  • in checks existence

8.Print the number of delivery items in the list.

CODE:

items=['Notebook', 'Pencil', 'Highlighter', 'Eraser', 'Marker', 'Glue Stick']
print(len(items))

OUTPUT:

6

EXPLANATION:

  • len() returns number of elements

9.Sort the list of items in alphabetical order and print the sorted list.

CODE:

items=['Notebook', 'Pencil', 'Highlighter', 'Eraser', 'Marker', 'Glue Stick']
items.sort()
print(items)

OUTPUT:

['Eraser', 'Glue Stick', 'Highlighter', 'Marker', 'Notebook', 'Pencil']

EXPLANATION:

  • sort() arranges alphabetically

10.The delivery man decides to reverse the order of his deliveries. Reverse the list and print it.

CODE:

items=['Eraser', 'Glue Stick', 'Highlighter', 'Marker', 'Notebook', 'Pencil']
items.reverse()
print(items)

OUTPUT:

['Pencil', 'Notebook', 'Marker', 'Highlighter', 'Glue Stick', 'Eraser']

EXPLANATION:

  • reverse() reverses order

11.Create a list where each item is a list containing a delivery item and its delivery time. Print the first item and its time.

CODE:

delivery = [["Notebook", "10AM"], ["Pencil", "11AM"]]
print(delivery[0])

OUTPUT:

['Notebook', '10AM']

EXPLANATION:

  • Nested list stores multiple values

12.Count how many times “Ruler” appears in the list and print the count.

CODE:

items=['Pencil', 'Notebook', 'Marker', 'Highlighter', 'Glue Stick', 'Eraser']
print(items.count("Ruler"))

OUTPUT:

0

EXPLANATION:

  • count() returns occurrences

13.Find the index of “Pencil” in the list and print it.

CODE:

items=['Pencil', 'Notebook', 'Marker', 'Highlighter', 'Glue Stick', 'Eraser']
print(items.index("Pencil"))

OUTPUT:

0

EXPLANATION:

  • index() finds position

14.Extend the list items with another list of new delivery items and print the updated list.

CODE:

items=['Pencil', 'Notebook', 'Marker', 'Highlighter', 'Glue Stick', 'Eraser']
items.extend(["Pen", "Sharpener"])
print(items)

OUTPUT:

['Pencil', 'Notebook', 'Marker', 'Highlighter', 'Glue Stick', 'Eraser', 'Pen', 'Sharpener']

EXPLANATION:

  • extend() adds multiple items

15.Clear the list of all delivery items and print the list.

CODE:

items=['Pencil', 'Notebook', 'Marker', 'Highlighter', 'Glue Stick', 'Eraser']
items.clear()
print(items)

OUTPUT:

[]

EXPLANATION:

  • clear() removes all items

16.Create a list with the item “Notebook” repeated three times and print the list.

CODE:

items = ["Notebook"] * 3
print(items)

OUTPUT:

['Notebook', 'Notebook', 'Notebook']

EXPLANATION:

  • * repeats list

17.Using a nested list comprehension, create a list of lists where each sublist contains an item and its length, then print the new list.

CODE:

items = ["Notebook", "Pencil"]
result = [[item, len(item)] for item in items]
print(result)

OUTPUT:

[['Notebook', 8], ['Pencil', 6]]

EXPLANATION:

  • Creates sublists with item + length

18.Filter the list to include only items that contain the letter “e” and print the filtered list.

CODE:

filtered = [item for item in items if "e" in item]
print(filtered)

OUTPUT:

['Notebook', 'Pencil']

EXPLANATION:

  • Filters based on condition

19.Remove duplicate items from the list and print the list of unique items.

CODE:

items = ["Notebook", "Pencil", "Notebook"]
unique = list(set(items))
print(unique)

OUTPUT:

['Notebook', 'Pencil']

EXPLANATION:

  • set() removes duplicates

Top comments (0)