Menu Close

How to choose longest string in list with Python?

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.

Posted in Python, Python Answers