0

I want to remove the whole list where the name is, given by the user

import taskk
agenda = []
count = 0
while True:
    print('---MENU---')
    print('Register- 1 / Remove - 2')
    option= int(input())
    if option== 1:
        name= input('Name:')
        wpp = int(input('Number: '))
        email = input('Digite o email: ')
        contact= [name,wpp,email]
        agenda.append(contact)
        count +=1
    if option == 2:
        name = input('Who do you want to remove? ')
        agenda.append(name)
    if count == 2:
        break

I want to do something like this, take the index from the name and add 1 to remove the name, wpp and email. I know it's wrong, but I figured I could do something like that

def remove(name, agenda = []):
    for registro in agenda:
            registro1 = agenda.index(name)
            registro2 = agenda.index(name+ 1)
            registro3 = agenda.index(name + 2)
            agenda.pop(registro1, registro2, registro3)
            return agenda
    return
Chris Maes
  • 35,025
  • 12
  • 111
  • 136
Tole
  • 25
  • 1
  • 1
  • 5
  • 1
    `.pop()` only takes at most 1 index. In any event -- why not return a new list rather than mutate the passed list? – John Coleman Dec 09 '19 at 14:17
  • 1
    Most likely you are looking for del or pop. Take a look at https://stackoverflow.com/questions/627435/how-to-remove-an-element-from-a-list-by-index – user5214530 Dec 09 '19 at 14:19
  • You can find the sub-list with the first element that equals `name` and remove that. For example `if registro[0] == name: agenda.remove(registro)` and `return` (assuming no duplicates). – 001 Dec 09 '19 at 14:25

2 Answers2

0

Try this:

def delete_contact(agenda, name):
    for contact in agenda[:]:
        if contact[0] == name:
           agenda.remove(contact)

and the agenda list will be updated without the need to return anything.

Sandra Sukarieh
  • 133
  • 1
  • 16
  • I try this but, how do I choose which name I want to delete?, if i have a list like this `[['we', 45, 'fgh@gdf'], ['hh', 45, 'gg@dd'], ['nnn', 45, 'aa@ss']]` and want to delete the sublist `['hh', 45, 'gg']` by name wich is the input `input('Who do you want to remove? ') ` – Tole Dec 10 '19 at 00:08
  • @Tole I did not get your comment here. Your question says that you want to delete a whole contact (which is a list of 3 items) from the agenda by letting the user enter the name of that contact, and as the name of the contact is the first element inside the contact list then you should use `contact[0]` when iterating over the contact objects of the agenda list. If you want to delete `'[hh', 45, 'gg']` then the input should be 'hh' (assuming there is no other contact with the same name). – Sandra Sukarieh Dec 11 '19 at 01:09
  • @Tole and in your code you should remove this line `agenda.append(name)` and call the `delete_contact(agenda, name)` with the name that the user enters. – Sandra Sukarieh Dec 11 '19 at 01:15
0

I'm assumming your agenda would contain a sub list which is '[name,wpp,email]' then when you want to remove, you can simply find the sub list with name=name_to_remove and remove it.

def remove(name, agenda):
    return [elem for elem in agenda if elem[0] != name]
NvN
  • 89
  • 1