3

My current plan is to determine which is the first entry in a number of Tkinter listboxes highlighted using .curselection() and combining all of the resulting tuples into a list, producing this:

tupleList = [(), (), ('24', '25', '26', '27'), (), (), (), ()] 

I'm wondering as to how to determine the lowest integer. Using .min(tupleList) returns only (), being the lowest entry in the list, but I'm looking for a method that would return 24.

What's the right way to get the lowest integer in any tuple in the list?

1
  • 1
    Instead of appending items to the list, use .extend method. You'll have a list of integers and then use min() to easily find the minimum. Commented May 1, 2012 at 11:13

3 Answers 3

6
>>> nums = [(), (), ('24', '25', '26', '27'), (), (), (), ()] >>> min(int(j) for i in nums for j in i) 24 
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you rubik and jamylak, both solutions are brilliant. Great thanks to gnibbler, a perfect solution for this situation without having to edit a thing.
@jamylak, Your solution would make sense if nums was being returned from a generator. Especially if it were dealing with a large amount of data
6
>>> from itertools import chain >>> nums = [(), (), ('24', '25', '26', '27'), (), (), (), ()] >>> min(map(int,chain.from_iterable(nums))) 24 

Comments

0
>>> min(reduce(lambda x, y: x + y, nums)) 

4 Comments

Doesn't work if nums has a number like '100' because it is checked alphabetically since it is still a str. Test it on nums = [(), (), ('24', '25', '26', '27'), (), (), (), (),('100',)] for example. Also be careful about using reduce because it is not liked in Python since there is usually a better option for almost every task.
Actually '100' is less than '24', I don't understand your point.
I'm pretty sure he wants to check the strings as numbers since he wants to find the minimum. He mentioned in the post: 'determine the lowest integer'
@jamylak There are no integers in the structure, however your comments make sense.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.