Python Forum
Removing existing tuples from a list of tuple
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Removing existing tuples from a list of tuple
#1
For one of my rev questions I am trying to write a function that updates a list of tuples whenever a user decides to add a new contact. This has to be done by removing a tuple if the new contact name already exists in the list and appending the new contact info into the list.

code so far

 def update_contact(list_of_tuples, search_name, new_phone):
        
        if search_name in list(sum(list_of_tuples, ())):
            x = list_of_tuples.index(search_name)
            list_of_tuples.remove(x)
            
            new_entry = (search_name, new_phone)
            list_of_tuples.append(new_entry)
            print(search_name + "'s phone number has been updated.")
        else:
            print(search_name, "is NOT found.")
Expected output:

data = [('Jill', '5239980'), ('Bob', '4562345'), ('Jenny', '2541273')]
    update_contact(data, 'Bob', '1111111')
    update_contact(data, 'Daniel', '2222222')
    
    Bob's phone number has been updated.
    Daniel is NOT found.
    Jill's phone number is 5239980
    Jenny's phone number is 2541273
    Bob's phone number is 1111111
    There are 3 contacts in the list.
Error:

x = list_of_tuples.index(search_name)
    ValueError: 'Bob' is not in list
I'm confused because Bob is in the list?
Reply


Messages In This Thread
Removing existing tuples from a list of tuple - by Bruizeh - May-15-2021, 04:08 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Homework - List containing tuples containing dicti Men 4 2,053 Dec-28-2021, 12:37 AM
Last Post: Men
  Removing all strings in a list that are of x length Bruizeh 5 3,249 Aug-27-2021, 03:11 AM
Last Post: naughtyCat
  Input validation for nested dict and sorting list of tuples ranbarr 3 3,948 May-14-2021, 07:14 AM
Last Post: perfringo
  Removing items in a list cap510 3 2,364 Nov-01-2020, 09:53 PM
Last Post: cap510
  Removing items from list slackerman73 8 4,480 Dec-13-2019, 05:39 PM
Last Post: Clunk_Head
  Generating a list and a tuple Truman 3 7,771 Mar-15-2018, 09:20 PM
Last Post: wavic
  list of list to list of tuple? student8 2 2,991 Nov-14-2017, 08:06 AM
Last Post: buran
  need help removing an item from a list jhenry 4 4,225 Oct-13-2017, 08:15 AM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020