bugbytes
bugbytes

Reputation: 335

Comparing elements in different lists python

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?

Upvotes: 2

Views: 120

Answers (2)

Michel Keijzers
Michel Keijzers

Reputation: 15357

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'

Upvotes: 0

squiguy
squiguy

Reputation: 33370

You can use max with a key.

max(a,b,c,d,key=len)

Upvotes: 11

Related Questions