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
#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


Messages In This Thread
RE: Removing existing tuples from a list of tuple - by deanhystad - May-15-2021, 07:14 PM

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