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
#2
'Bob' is not in the list
print('Bob' == ('Bob', '4562345'))
Output:
False
('Bob', '4562345') is in the list
data = [('Jill', '5239980'), ('Bob', '4562345'), ('Jenny', '2541273')]
print(data.index(('Bob', '4562345')))
Output:
1
To find the index of the tuple that 'Bob' is inside you would need to loop through the list comparing the first index item of each against 'Bob', you can use enumerate to get the index as you loop through.
Reply
#3
Seems to me the behavior would be better served by using a dictionary instead of tuples. Is that allowed? I always hate questions that restrict you from doing the best practices, essentially teaching you to do the wrong things.
Reply
#4
(May-15-2021, 02:15 PM)jefsummers Wrote: Seems to me the behavior would be better served by using a dictionary instead of tuples. Is that allowed? I always hate questions that restrict you from doing the best practices, essentially teaching you to do the wrong things.

I was actually reading up on dictionaries earlier and yeah it would seem better suited, as I'm essentially associating the name key with the number value. I found a way to do it with dicts and eventually did something similar to fix my issue here. I think they just want to ensure we understand taught material by making us use it to solve problems that could easily be done using different material. A bit of a pain still
Reply
#5
Searching and sorting collection or composite objects based one one of their attributes is a common programming problem. A dictionary would work better than tuples in this example, but what your items were personnel records and you wanted to find all employees who have worked for the company more than 10 years? Can you use a dictionary to do that? No, it would not. Sometime, eventually, you will need a way peek into an object and use that in your search or sort.

You could write a loop that looks at each tuple in the list, compares the employee name to the new contact name, and removes the tuple if they match. But what if you later wanted to search tuples to find a matching number (phone? ID?). That would require writing a different loop. A more realistic employee record might require dozens of different loops to handle all the possible searches.

A better method is to separate the comparison from the search. Can you think of a way where you could pass a function, maybe in the form of a lambda expression, as an argument to your search routine? Then you could have 1 search or that will work for searching any attribute. I mentioned sorting before because sorting has a feature like this. Maybe you can write your own "index" function that takes a "key" like the sort function?
Reply


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