Velocity Reviews > max value in list

# max value in list

C GIllespie
Guest
Posts: n/a

 05-12-2004
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

Terry Reedy
Guest
Posts: n/a

 05-12-2004

"C GIllespie" <(E-Mail Removed)> wrote in message
news:c7te26\$8q1\$(E-Mail Removed)...
> 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

Jean Brouwers
Guest
Posts: n/a

 05-12-2004
[[ This message was both posted and mailed: see
the "To," "Cc," and "Newsgroups" headers for details. ]]

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 <c7te26\$8q1\$(E-Mail Removed)>, C GIllespie
<(E-Mail Removed)> 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
>
>

Peter Abel
Guest
Posts: n/a

 05-12-2004
"C GIllespie" <(E-Mail Removed)> wrote in message news:<c7te26\$8q1\$(E-Mail Removed)>...
> 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

Peter Otten
Guest
Posts: n/a

 05-12-2004
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

Duncan Booth
Guest
Posts: n/a

 05-12-2004
"Terry Reedy" <(E-Mail Removed)> wrote in
news:(E-Mail Removed):

> 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
>>>

C GIllespie
Guest
Posts: n/a

 05-13-2004
Dear All,

Thanks for all the responses.

Colin