Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8840941
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T10:35:13+00:00 2026-06-14T10:35:13+00:00

Possible Duplicate: Python’s most efficient way to choose longest string in list? I have

  • 0

Possible Duplicate:
Python’s most efficient way to choose longest string in list?

I have a list L

L = [[1,2,3],[5,7],[1,3],[77]]

I want to return the length of the longest sublist without needing to loop through them, in this case 3 because [1,2,3] is length 3 and it is the longest of the four sublists. I tried len(max(L)) but this doesn’t do what I want. Any way to do this or is a loop my only way?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T10:35:14+00:00Added an answer on June 14, 2026 at 10:35 am

    max(L,key=len) will give you the object with the longest length ([1,2,3] in your example) — To actually get the length (if that’s all you care about), you can do len(max(L,key=len)) which is a bit ugly — I’d break it up onto 2 lines. Or you can use the version supplied by ecatamur.

    All of these answers have loops — in my case, the loops are implicit which usually means they’ll be executed in optimized native machine code. If you think about it, How could you know which element is the longest without looking at each one?


    Finally, note that key=function isn’t a feature that is specific to max. A lot of the python builtins (max,min,sorted,itertools.groupby,…) use this particular keyword argument. It’s definitely worth investing a little time to understand how it works and what it typically does.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Best way to strip punctuation from a string in Python I have
Possible Duplicate: Python: What is the best way to check if a list is
Possible Duplicate: Python, compute list difference I have two lists For example: A =
Possible Duplicate: Python: simple list merging based on intersections I have a multiple list:
Possible Duplicate: python dict.add_by_value(dict_2) ? My input is two dictionaries that have string keys
Possible Duplicates: Split python string every nth character? What is the most “pythonic” way
Possible Duplicate: Python - Differences between elements of a list I have a list
Possible Duplicate: python max of list of arrays I have a list of arrays
Possible Duplicate: Python : how to append new elements in a list of list?
Possible Duplicate: Python: Get object by id Typically when you see the string representation

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.