Find length of each string in a list python

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?

codeape

95.4k24 gold badges151 silver badges180 bronze badges

asked Oct 27, 2009 at 9:50

3

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.

answered Oct 27, 2009 at 10:02

SilentGhostSilentGhost

294k64 gold badges301 silver badges291 bronze badges

0

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/

answered Oct 27, 2009 at 9:54

codeapecodeape

95.4k24 gold badges151 silver badges180 bronze badges

2

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

UPDATE: SilentGhost's solution is a lot nicer.

answered Oct 27, 2009 at 10:05

Tarnay KálmánTarnay Kálmán

6,8605 gold badges43 silver badges56 bronze badges

just ask for the max according to the length

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

answered Oct 27, 2009 at 10:01

chubchub

7576 silver badges10 bronze badges

0

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)

answered Dec 13, 2021 at 22:25

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]

answered Oct 27, 2009 at 9:57

3

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))

answered Apr 24, 2015 at 8:03

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))

answered Dec 29, 2020 at 14:35

How do I find the length of a string in a list Python?

A list is identifiable by the square brackets that surround it, and individual values are separated by a comma. To get the length of a list in Python, you can use the built-in len() function. Apart from the len() function, you can also use a for loop and the length_hint() function to get the length of a list.

How do I find the length of a string in a list?

The len() function for getting the length of a list. Python has a built-in function len() for getting the total number of items in a list, tuple, arrays, dictionary etc. The len() method takes an argument where you may provide a list and it returns the length of the given list.

Can you use LEN () on a list?

The function len() is one of Python's built-in functions. It returns the length of an object. For example, it can return the number of items in a list. You can use the function with many different data types.

How does LEN () work in Python?

Python len() Function The len() function returns the number of items in an object. When the object is a string, the len() function returns the number of characters in the string.