0

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) 
24
  • Welcome to SO. Firstly, I have to say that in your example the list comprehension does not differ from mylist because 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 the min function works on tuples: used this way it simply gives you the tuple with the smallest first element, which is Part1. You can test it by simply naming it Part7 or whatever, and the result of min will be ('part2', 7, 7). Commented Feb 4, 2020 at 8:58
  • Besides all that, you should think about how you would like to define the minimum of your list elements. If you can express this idea in a function, you can use this function as a key parameter of the min function so it will search for the minimum according to your needs. Commented Feb 4, 2020 at 9:01
  • HI @SpghttCd Thanks for welcoming Its a Typo on part 5. anyway if the min function does not give me the result i am after then what should i use? Commented Feb 4, 2020 at 9:02
  • As I said, you can use the min function. But its standard sorting algorithm for tuples (order by first elements) seems not to be what you need. So you have to think how you want a list of three-element-tuples to be sorted to be able to name one or more of them "minimal": e.g. is ('partx', 4, 6) to be considered smaller than ('partx', 5, 5)? or equal? is ('partx', 4, 5) equal to ('partx', 5, 4)? ... ... Commented Feb 4, 2020 at 9:08
  • Its not about the sorting @SpghttCd i am not getting all the tuples back in the list I would like to get all the tuples back that meet the requirements Using result = min([x for x in mylist if x[1] >= 4 and x[2] >= 4]) already defines that i would like to get back the value closest to my input So my input is 4 and the closest to that is is 5 If my input would be 10 and 8 then i would only get back ('Part3', 11, 9) Since this is the closest to and higher then Commented Feb 4, 2020 at 9:14

1 Answer 1

1
threshold = 4 mylist = [('Part1', 5, 5), ('Part2', 7, 7), ('Part3', 11, 9), ('Part4', 45, 45), ('part5', 5, 5)] filtered = [x for x in mylist if x[1] >= threshold and x[2] >= threshold] keyfunc = lambda x: x[1] my_min = keyfunc(min(filtered, key=keyfunc)) result = [v for v in filtered if keyfunc(v)==my_min] # [('Part1', 5, 5), ('part5', 5, 5)] 
Sign up to request clarification or add additional context in comments.

4 Comments

who doesn't...? And it's quite fun, as far as I can tell of what I learned so far...
True Its a life time learning And yes i agree its fun but can be very challenging sometimes
yep. the biggest challenge in the end often resides right in the middle between our own ears :-)
xDxD true that will always be the biggest one. Specifically to keep it cool :-)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.