S7nf
S7nf

Reputation: 83

How to find length of an element in a list?

I'm just starting with programming. I have a list of a few strings and now I need to print the biggest (in length) one. So I first want to just print the lengths of elements. I was trying things like this:

l = ("xxxxxxxxx", "yyyy","zz")

for i in range(len(l)):

So how do I do it?

Upvotes: 8

Views: 100160

Answers (8)

Tambe Tabitha
Tambe Tabitha

Reputation: 175

For those who are here because they want to measure the lengths of all the elements in a sequence(list,tuple, etc) and return those lengths into another sequence (list, tuple etc), do this:

TLDR

list_of_lengths = (lambda x:[len(i) for i in x])(lst)

Longer explanation (from the inner brackets, moving outwards)

  1. We loop through our list and get the corresponding length value of each element using the len() function which is inbuilt into Python.
[len(i) for i in x]
  1. Create a temporary function to wrap the looping operation in the previous point so that any list can be put into the function and its element lengths returned.
(lambda x:[len(i) for i in x])
  1. Call the function using our list as the argument by putting it in brackets next to the function definition.
(lambda x:[len(i) for i in x])(lst)
  1. Assign the newly created list to variable so that you can use it for other operations (like finding the largest/smallest element or its index as seen in the question asked on this page.)
list_of_lengths = (lambda x:[len(i) for i in x])(lst)

Upvotes: 5

Saviour24
Saviour24

Reputation: 56

First of all to find the length of strings you can define the function and use map function as below:

def len_words(l:list):
    len_list = list(map(len,l))
    return len_list

After that you can call the max function on len_words to find the maximum value of string from list.

max(len_words(l))

Upvotes: 0

This code maps "len" function over the list, to get another list containing the length of every item.

mylist = ("xxxxxxxxx", "yyyy","zz")
len_mylist = map(len, mylist)
max_position = len_mylist.index(max(len_mylist))
my_item = mylist[max_position]
print('The largest item of my list is: ' + my_item)
print('located at position: ' + str(max_position))

Upvotes: 0

codeape
codeape

Reputation: 100904

To print the lengths of the elements:

elements = ["xxxxxx", "yyy", "z"]
for element in elements:
    print len(element)

I recommend you read some tutorial material, for instance http://docs.python.org/tutorial/

Upvotes: 9

Tarnay Kálmán
Tarnay Kálmán

Reputation: 7066

>>> sorted(['longest','long','longer'],key=len)[-1]
'longest'

UPDATE: SilentGhost's solution is a lot nicer.

Upvotes: 4

SilentGhost
SilentGhost

Reputation: 320019

l = ("xxxxxxxxx", "yyyy","zz")
print(max(l, key=len))

First of all you don't have a list, you have a tuple. this code will work for any sequence, however; both lists and tuples are sequences (as well as strings, sets, etc). So, the max function takes a key argument, that is used to sort the elements of an iterable. So, from all elements of l will be selected the one having the maximum length.

Upvotes: 27

chub
chub

Reputation: 787

just ask for the max according to the length

print max(["qsf","qsqsdqsd","qs"], key = len)

Upvotes: 3

user59634
user59634

Reputation:

The following code will print the largest string in the set:

l = ["abcdev", "xx","abcedeerer"]
len_0 = len(l[0])
pos = 0

for i in range(0,len(l)):
        if len(l[i]) > len_0:
                pos = i
print l[pos]

Upvotes: 0

Related Questions