Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Hello, sriram! I did the foloowing code very rapidly, so I am not sure if it is optimal or not, but it works.

Let's define the following function:

def find_max_len(L):
    max_len = 0 # this stores the maximum length
    max_lis = [] # this stores the maximum length lists

    for li in L:
        n = len(li) # the length of the current list
        if n > max_len: # if the current list has a length grater than what we thought was the maximum length...
            max_len = n # update the maximum length
            max_lis = [li] # discard the previous lists (which are shorter), and include the current one
        elif n == max_len: # if the current list has the same length as what we think to be the maximum length...
            max_lis.append(li) # add the current list to the set of maximum length lists

    print('Maximum length:', max_len) # print the maximum length
    for li in max_lis:
         print(li, end='  ') # print the maximum length lists, separated by two spaces
    print() # print a new empty line

Now, you can do the following:

L=[[1,2,3,4],[1,2],[1,4,5,6],[9,10,12,8],[7,3],[4,5,6]]
find_max_len(L)

The output is:

Maximum length: 4
[1, 2, 3, 4]  [1, 4, 5, 6]  [9, 10, 12, 8]

I assume there should be a better programatical way of doing this (perhaps with list comprehensions), but I didn't have the time to explore that possibility. If I find another way to do this, I'll post it as an update.

I hope this helps!