I have a list of tuples. Every tuple has 5 elements [corresponding to 5 database columns] and I'd like to make a query
select attribute1 from mylist where attribute2 = something
e.g.
personAge = select age from mylist where person_id = 10
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:
results = [t.age for t in mylist if t.person_id == 10]
Otherwise use indexes:
results = [t[1] for t in mylist if t[0] == 10]
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 [person_id, age, _, _, _, _]
to unpack a six item tuple.
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:
>>> mylist = [[25,7],[26,9],[55,10]]
>>> [age for [age,person_id] in mylist if person_id == 10]
[55]
Another way would be using map
and filter
:
>>> map[ lambda [age,_]: age, filter[ lambda [_,person_id]: person_id == 10, mylist] ]
[55]
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:
filter[lambda t: t[0]==10, mylist]
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]:
[age] = [t[1] for t in mylist if t[0]==10]
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:
mylist = [[25,7],[26,9],[55,10]]
[age for [age,person_id] in mylist if any[person_id == 10]]
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 to select from list by condition in Python?
To select from list by condition in Python, use list comprehension or the filter[] function. A third way of solving this problem, is the use of loops as shown in example 3. However, this is essentially just a longer way to write a list comprehension.solution 1: list comprehension
List comprehension can be used to apply rules to a list and select specific elements from a list by conditions. In the example shown below, only even numbers are selected from the list. This is achieved by using the modulo operator. By definition a number is even, if it's divided by 2 and the remainder is 0. The list comprehension below checks for the remainder and adds an element to the evenList only if the remainder is 0.# select from list by condition with list comprehension myList = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] evenList = [num for num in myList if num % 2 == 0] print[evenList]
In this example, myList is populated with integers between 1 and 10. Then, list comprehension is applied to check the remainder of the division by 2. If the remainder is 0, the element is added to the list. Finally, the evenList is printed and the result is shown below. [2, 4, 6, 8, 10]solution 2: filter[] function
Use the built-in filter[] function together with a lambda function and the list[] function.# select from list by condition with filter[] function myList = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] evenList = list[filter[lambda num: num % 2 == 0, myList]] print[evenList]
In this example, myList is populated with integers between 1 and 10 - similar to solution 1. Then a lambda function is used to check if the remainder of the division by 2 is 0. If the remainder is 0, the element passes the filter. The filter[] functions returns a filter object. The list[] function is used, to turn the filter object back into a list. [2, 4, 6, 8, 10]solution 3: loops
A third solution to the problem of selecting from a list by conditions is the use of loops. It is the same principle that is used in the solution 1 list comprehension. It is however a longer way to write the same functionality and therefore considered less pythonic. The use of loops to select from a list by condition is shown below for comparison.# select from list by using loops myList = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] evenList = [] for num in myList: if num % 2 == 0: evenList.append[num] print[evenList]
In this example, myList is populated with integers between 1 and 10 - similar to the solutions shown above. Then, an empty list named evenList is defined. This list will be used to store the even elements of myList. Then, a for loop is used to iterate through elements of myList. If an element is divided by 2 and the remainder is 0 it means the element is even. Even elements are appended to the evenList within the if statement. Finally, the evenList is printed and the result is shown below. [2, 4, 6, 8, 10]
FAQ
How to select from list by condition?
To select from list by condition use the following syntax: [num for num in myList if num % 2 == 0]
How to filter a list by condition?
To filter a list by condition use the list function. Example: filter[lambda num: num % 2 == 0, myList]