1

Let's say I have a list:

def returnBiggestStartingList:

   L1 = [ [1,2,3,4], [5,6,7,8], [7,8,9,10] ]

I want to return one of the lists inside L1. The one I want to return is the one where the 0th element is the biggest. For example, in the example above I want to return L1[2] because it's first element is higher than the first element of all the other arrays (7 is bigger than 1 and 5).

I also need to account for if the 0th element is the same in one or more lists, if that is the case I would move onto the second element and compare those (so on and so forth)

Anyone know how I can do this?

2 Answers2

6

What you're describing is the natural order of the lists. So you can use max directly, and it's even simpler if all lists have the same size (when the lists have different lengths,
if a list is shorter, it will compare lower to a longer list with same first elements ex: [7,8,9] < [7,8,9,10] which should be OK for your needs too)

L1 = [ [1,2,3,4], [5,6,7,8], [7,8,9,11], [7,8,9,10] ]

print(max(L1))

result:

[7, 8, 9, 11]
Jean-François Fabre
  • 137,073
  • 23
  • 153
  • 219
  • This looks right to me ... But I'm not sure what the _size_ of the list has to do with anything ;-) – mgilson May 23 '17 at 21:16
  • 1
    if a list is shorter, it will compare lower to a longer list with same first elements. But that's probably also expected. – Jean-François Fabre May 23 '17 at 21:17
  • This is perfect.. thank you very much ! –  May 23 '17 at 21:19
  • 1
    So? Python will happily compare them and give a deterministic result: `max([1, 2, 3, 4], [1, 2, 3])` --> `[1, 2, 3, 4]`. Since OP says that the comparison is only based on the first element (and doesn't provide any conflict resolution strategy when the first elements are equal), presumably any of the lists with maximal first element would be OK ... :-) – mgilson May 23 '17 at 21:19
  • @mgilson: quoting OP question "I also need to account for if the 0th element is the same in one or more lists, if that is the case I would move onto the second element and compare those (so on and so forth)". Natural order of lists couldn't be described better. – Jean-François Fabre May 23 '17 at 21:20
  • maybe I shouldn't talk about lists sizes at all in my answer? OP seems to have list elements of the same size. – Jean-François Fabre May 23 '17 at 21:20
  • 2
    @Jean-FrançoisFabre -- Ahh, I missed that. It's still just lexicographic ordering as far as has been specified in the question :-). Yeah, dropping the statement about the lengths is what I'm advocating. At best, I don't think it adds anything to the answer. At worst, it feels a little confusing because it doesn't seem like the length matters. With that said, I don't think it _really_ matters much either way... – mgilson May 23 '17 at 21:22
  • @mgilson I have removed "complicated" :) – Jean-François Fabre May 23 '17 at 21:23
  • @Jean-FrançoisFabre -- Sure, works for me :). – mgilson May 23 '17 at 21:24
  • Yes the lists are all the same size - thank you :D –  May 23 '17 at 21:24
  • @asdfghjkl then you can accept the answer if it fits :) – Jean-François Fabre May 23 '17 at 21:36
0

You can try this:

 def returnBiggestStartingList(L1):

     return [i for i in L1 if i[0] == max(zip(*L1)[0])][0]
Ajax1234
  • 69,937
  • 8
  • 61
  • 102