Wednesday, July 31, 2019

Insert, Count, Remove, Append, Index, Reverse, Add and Pop from a list

Script

#!/usr/local/bin/python3.7

grocery_list = ['banana', 'apple', 'tomato', 'potato', 'cabbage', 'radish', 'cucumber', 'capsicum', 'ginger', 'carrot', 'bean', 'onion']

print()
print(grocery_list)

print()
print("Insert Spinach in position 3")
grocery_list.insert(3, 'spinach')

print(grocery_list)

print()
print("Display the number of occurrences of Radish and Ginger")
print("Entries for Radish: ", grocery_list.count('radish'), "     Entries for Ginger:" , grocery_list.count('ginger'))

grocery_list.remove('cucumber')

print()
print("Remove Cucumber from the list")
print(grocery_list)
print()

grocery_list.append('okra')

print("Append Okra to the list")
print(grocery_list)
print()

print("Index Capsicum in the list")
print(grocery_list.index('capsicum'))
print()

grocery_list.reverse()

print("Reverse the list")
print(grocery_list)
print()

print("Add Beetroot and Lady's Finger to the list")
x = ['beetroot', 'ladys-finger']
grocery_list.extend(x)

print(grocery_list)
print()

grocery_list.pop()
print("Pop out the last item in the list")

print(grocery_list)
print()

Execution

['banana', 'apple', 'tomato', 'potato', 'cabbage', 'radish', 'cucumber', 'capsicum', 'ginger', 'carrot', 'bean', 'onion']

Insert Spinach in position 3
['banana', 'apple', 'tomato', 'spinach', 'potato', 'cabbage', 'radish', 'cucumber', 'capsicum', 'ginger', 'carrot', 'bean', 'onion']

Display the number of occurrences of Radish and Ginger
Entries for Radish:  1      Entries for Ginger: 1

Remove Cucumber from the list
['banana', 'apple', 'tomato', 'spinach', 'potato', 'cabbage', 'radish', 'capsicum', 'ginger', 'carrot', 'bean', 'onion']

Append Okra to the list
['banana', 'apple', 'tomato', 'spinach', 'potato', 'cabbage', 'radish', 'capsicum', 'ginger', 'carrot', 'bean', 'onion', 'okra']

Index Capsicum in the list
7

Reverse the list
['okra', 'onion', 'bean', 'carrot', 'ginger', 'capsicum', 'radish', 'cabbage', 'potato', 'spinach', 'tomato', 'apple', 'banana']

Add Beetroot and Lady's Finger to the list
['okra', 'onion', 'bean', 'carrot', 'ginger', 'capsicum', 'radish', 'cabbage', 'potato', 'spinach', 'tomato', 'apple', 'banana', 'beetroot', 'ladys-finger']

Pop out the last item in the list
['okra', 'onion', 'bean', 'carrot', 'ginger', 'capsicum', 'radish', 'cabbage', 'potato', 'spinach', 'tomato', 'apple', 'banana', 'beetroot']







No comments:

Post a Comment