Python select element from list with condition
I have a list of tuples. Every tuple has 5 elements (corresponding to 5 database columns) and I'd like to make a query Show
e.g.
Is it possible to query the list of tuples in some way?
Georgy 10.8k7 gold badges62 silver badges68 bronze badges asked Oct 24, 2011 at 13:22
If you have named tuples you can do this:
Otherwise use indexes:
Or use tuple unpacking as per Nate's answer. Note that you don't have to give a meaningful name to every item you unpack. You can do answered Oct 24, 2011 at 13:27
Steven RumbalskiSteven Rumbalski 43.1k8 gold badges85 silver badges117 bronze badges 0
One solution to this would be a list comprehension, with pattern matching inside your tuple:
Another way would be using
answered Oct 24, 2011 at 13:29
NateNate 12.3k5 gold badges43 silver badges60 bronze badges Yes, you can use filter if you know at which position in the tuple the desired column resides. If the case is that the id is the first element of the tuple then you can filter the list like so:
This will return the list of corresponding tuples. If you want the age, just pick the element you want. Instead of filter you could also use list comprehension and pick the element in the first go. You could even unpack it right away (if there is only one result):
But I would strongly recommend to use dictionaries or named tuples for this purpose. answered Oct 24, 2011 at 13:32
rplntrplnt 2,33115 silver badges14 bronze badges 4 Building on Nate's answer: If your list has tuples with non-unique values, this might produce a boolean ValueError (truth value of a Series is ambiguous). To circument that:
Obivously for a "person_id" this less intuitive, but imagine you switch the query: finding all person_IDs for people of a given age. answered May 14, 2021 at 15:46
KingOttoKingOtto 6491 silver badge15 bronze badges
How do you select a specific item in a list Python?How to Get Specific Elements From a List?. Get elements by index. use the operator [] with the element's index. use the list's method pop(index) use slicing lst[start:stop:step] to get several elements at once. ... . Get elements by condition. use the filter() function. use a list comprehension statement.. How do you filter a list based on a condition in Python?Use list comprehension to filter a list. Use the syntax [item for item in list if condition] with list as a list and condition as a boolean expression to create a list containing each element in list that satisfies condition .
How do you check if every element of a list satisfies a condition in Python?Use all() to check if every element of a list satisfies a condition. Use the syntax condition for item in list to build a generator expression that checks each item in list against a given condition .
How you can filter out element from a list?Python has a built-in function called filter() that allows you to filter a list (or a tuple) in a more beautiful way. The filter() function iterates over the elements of the list and applies the fn() function to each element. It returns an iterator for the elements where the fn() returns True .
|