I have a list that looks like this
mylist = [('Part1', 5, 5), ('Part2', 7, 7), ('Part3', 11, 9), ('Part4', 45, 45), ('part5', 5, 5)]
I am looking for all the tuples that has a number closest to my input
now i am using this code
result = min([x for x in mylist if x[1] >= 4 and x[2] >= 4]) The result i am getting is
('part5', 5, 5)
But i am looking for an result looking more like
[('Part1', 5, 5), ('part5', 5, 5)]
and if there are more tuples in it ( i have 2 in this example but it could be more) then i would like to get all the tuples back
the whole code
mylist = [('Part1', 5, 5), ('Part2', 7, 7), ('Part3', 11, 9), ('Part4', 45, 45), ('part5', 5, 5)] result = min([x for x in mylist if x[1] >= 4 and x[2] >= 4]) print(result)
mylistbecause all numbers are bigger than 4 in your example data. Second: No your code does not return('part5', 5, 5)but('part1', 5, 5). Third: I think you misunderstand how theminfunction works on tuples: used this way it simply gives you the tuple with the smallest first element, which isPart1. You can test it by simply naming itPart7or whatever, and the result ofminwill be('part2', 7, 7).keyparameter of theminfunction so it will search for the minimum according to your needs.