Categories
Python Answers

How to choose longest string in list with Python?

Spread the love

Sometimes, we want to choose longest string in list with Python.

In this article, we’ll look at how to choose longest string in list with Python.

How to choose longest string in list with Python?

To choose longest string in list with Python, we can use the max function with the key argument set to len.

For instance, we write

my_list = ['123','123456','1234']
print(max(my_list, key=len))

to call max with my_list with key set to len to get the longest string from my_list.

Conclusion

To choose longest string in list with Python, we can use the max function with the key argument set to len.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *