Skip to content

Instantly share code, notes, and snippets.

@shinysu
Last active November 18, 2020 12:26
Show Gist options
  • Select an option

  • Save shinysu/5889db70186e9403e93029c89849e840 to your computer and use it in GitHub Desktop.

Select an option

Save shinysu/5889db70186e9403e93029c89849e840 to your computer and use it in GitHub Desktop.
Examples & exercises using list
'''
check if an element is present in the list
you can use 'in' operator to check if the element is present and 'not in' operator to check if the element is not present
'''
cart = ['ice cream', 'chocolates', 'bread', 'jam', 'butter']
item = input("Enter the element you want to search: ")
if item in cart:
print("yes, the element is present")
else:
print("No, the element is not present")
'''
iterate through the list and display elements in the list
'''
cart = ['ice cream', 'chocolates', 'bread', 'jam', 'butter']
for x in cart:
print(x)
def add_item():
item = input("Enter the task: ")
todolist.append(item)
print(todolist)
def delete_item():
item = input("Enter the element to be removed: ")
todolist.remove(item)
print(todolist)
todolist =[]
while True:
choice = input("Enter your choice(add / delete / exit): ").lower()
if choice == 'add':
add_item()
elif choice == 'delete':
delete_item()
elif choice == 'exit':
break
else:
print("Invalid choice")
In the todolist program, include two more options:
1. edit - To edit an item in the todolist
- Get the old item and the new item from the user.
- Remove the old item from the list and add the new item
2. display - Display all the elements in the list in sorted order
- Sort the list
- display the elements in the list using for loop
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment