By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
439,932 Members | 1,944 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 439,932 IT Pros & Developers. It's quick & easy.

max value in list

P: n/a
Dear all,

I have a few lists. What's the best way of finding the maximum number of all
the lists.

For example,

[2,3,4],[1,2,3],[4,7]

The max is 7.

Thanks

Colin
Jul 18 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
[[ This message was both posted and mailed: see
the "To," "Cc," and "Newsgroups" headers for details. ]]
What about:

m = max([max(i) for i in (list1, list2, list3, etc...)])

Example:
max([max(i) for i in ([2,3,4], [1,2,3], [4,7])])
7

/Jean Brouwers
ProphICy Semiconductor, Inc.
In article <c7**********@ucsnew1.ncl.ac.uk>, C GIllespie
<cs******@hotmail.com> wrote:
Dear all,

I have a few lists. What's the best way of finding the maximum number of all
the lists.

For example,

[2,3,4],[1,2,3],[4,7]

The max is 7.

Thanks

Colin

Jul 18 '05 #2

P: n/a

"C GIllespie" <cs******@hotmail.com> wrote in message
news:c7**********@ucsnew1.ncl.ac.uk...
Dear all,

I have a few lists. What's the best way of finding the maximum number of all the lists.

For example,

[2,3,4],[1,2,3],[4,7]

The max is 7.


Sidestepping 'best' which may depend on context, a general 'good' way is
lists = [2,3,4],[1,2,3],[4,7]
max([max(l) for l in lists])

7

Terry J. Reedy


Jul 18 '05 #3

P: n/a
"C GIllespie" <cs******@hotmail.com> wrote in message news:<c7**********@ucsnew1.ncl.ac.uk>...
Dear all,

I have a few lists. What's the best way of finding the maximum number of all
the lists.

For example,

[2,3,4],[1,2,3],[4,7]

The max is 7.

Thanks

Colin

l=[[2, 3, 4], [1, 2, 9], [4, 7]]
max(map(max,l)) 9 l=[[2, 3, 4], [1, 2, 9],[12], [4, 7]]
max(map(max,l)) 12


Regards
Peter
Jul 18 '05 #4

P: n/a
C GIllespie wrote:
I have a few lists. What's the best way of finding the maximum number of
all the lists.

For example,

[2,3,4],[1,2,3],[4,7]

The max is 7.


For arbitrarily nested sequences:
def rmax(seq): .... try:
.... return max(map(rmax, seq))
.... except TypeError:
.... return seq
.... rmax(1) 1 rmax([1,2]) 2 rmax([[1,2], [3,4]]) 4 rmax([[1,2], [3,44], 5, [6]]) 44 rmax([[1,2], [[3,44], 5, [6]]]) 44


Peter

Jul 18 '05 #5

P: n/a
"Terry Reedy" <tj*****@udel.edu> wrote in
news:ma**************************************@pyth on.org:
Sidestepping 'best' which may depend on context, a general 'good' way is
lists = [2,3,4],[1,2,3],[4,7]
max([max(l) for l in lists]) 7


Without any claims to better or worse, here is another option:
l = [2,3,4],[1,2,3],[4,7]
l ([2, 3, 4], [1, 2, 3], [4, 7]) max(itertools.chain(*l)) 7

Jul 18 '05 #6

P: n/a
Dear All,

Thanks for all the responses.

Very helpful (again)

Colin

Jul 18 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.