Ask Your Question

How to find all maximum length lists with a nested and display?

asked 2020-07-04 17:45:36 +0100

sriram gravatar image

How to find all maximum length lists with a nested and display?


Now i need to find the maximum length lists in this

that the program has to display

[1,2,3,4] [1,4,5,6] [9,10,12,8] and say maximum length is 4
I will input some arbitrary nested list L like above and it has to do this As i am novice to sagemath kind help

Or can we sort the nested list based on its inner lists lengths

edit retag flag offensive close merge delete

3 Answers

Sort by ยป oldest newest most voted

answered 2020-07-04 19:02:31 +0100

curios_mind gravatar image

As the other contributors have said, you can simply use python.

Just another version:

givememaxlen=lambda x: max([len(i) for i in x]);
edit flag offensive delete link more

answered 2020-07-04 18:56:35 +0100

philipp7 gravatar image

updated 2020-07-04 18:57:52 +0100

You don't need Sage for that, you can use pure Python. Here is a solution with list comprehensions:

L = [[1,2,3,4],[1,2],[1,4,5,6],[9,10,12,8],[7,3],[4,5,6]]
lengths = list(len(l) for l in L)

maxLength = max( len(l) for l in L )

listWithMaxLength = list(l for l in L if len(l) == maxLength)

First we create the list lengths with the lengths of the individual lists. Then we can simply get the maximum length as the maximum of that list. Now we create a new list listWithMaxLength where we put all the lists from L in which have this maximum length (for this you can also use the method filter from python). I'm sure there are faster methods to do that but for reasonable inputs this should work absolutely fine.

edit flag offensive delete link more

answered 2020-07-04 18:45:10 +0100

dsejas gravatar image

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:


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!

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower


Asked: 2020-07-04 17:45:36 +0100

Seen: 905 times

Last updated: Jul 04 '20