2

I have four elements to compare with

A= [1,2,3,4]
B=[1,2]
C= [3,5,6]
D= [2, 4, 5, 6,7]

How do I compare which one has the greatest len?

2 Answers 2

11

You can use max with a key.

max(a,b,c,d,key=len)
Sign up to request clarification or add additional context in comments.

2 Comments

I like this one line solution! neat! Well it is python after all
I always forget about the key keywords. I really need to start making better use of this.
0

You can use the len function, e.g.

len(A) 

gives

4

So it would be something like:

len_a = len(A)
len_b = len(B)
len_c = len(C)
len_d = len(D)

max_length = max([len_a, len_b, len_c, len_d])

if len_a == max_length:
    print 'A'
if len_b == max_length:
    print 'B'
if len_c == max_length:
    print 'C'
if len_c == max_length:
    print 'D'

2 Comments

what if A and B have the same length?
@user2626540: edited ... now it gives multiple items if they have the same length.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.