Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to remove an item from a list with enumeration starting at one?

if main == 'remove':
    for count, item in  enumerate(grocery_list, 1):
        print(f'{count}. {item}')
    which_item = input('Which item do you want to remove? Type in the name of the item please! ')
    del grocery_list[int(which_item-1)]
    print('Your item has been removed! ')
    continue

I’m trying to let user remove an item by typing in the enumerated index. When they do type in remove, it give them a list like this:

  1. item
  2. item
  3. item

I tried to do del grocery_list[int(which_item-1)] but that gave an error. I want one subtracted from the which_item variable.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

If position of item is asked to the user:

if main == 'remove':
    for count, item in enumerate(grocery_list, 1):
        print(f'{count}. {item}')
    which_item = int(input('Which item do you want to remove? Type in the position of the item please! '))
    grocery_list.pop(which_item-1)
    print('Your item has been removed! ')

If value of item is asked to the user:

if main == 'remove':
    for count, item in enumerate(grocery_list, 1):
        print(f'{count}. {item}')
    which_item = input('Which item do you want to remove? Type in the name of the item please! ')
    grocery_list.remove(which_item)
    print('Your item has been removed! ')
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading