471,337 Members | 1,355 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,337 software developers and data experts.

lists of lists

brand new to python so please be gentle! i have a function that returns a list of lists, but now i need to access and print the biggest list. i have started off with this:

for list in listoflists:
>>>size = len(list)

and then i want to do something like compare all the sizes and just print the biggest one, but i can't get my head around the syntax. or should i be going about this a completely different way?

any advice gratefully received, thanks :)
Jun 14 '07 #1
7 1297
bvdet
2,851 Expert Mod 2GB
brand new to python so please be gentle! i have a function that returns a list of lists, but now i need to access and print the biggest list. i have started off with this:

for list in listoflists:
>>>size = len(list)

and then i want to do something like compare all the sizes and just print the biggest one, but i can't get my head around the syntax. or should i be going about this a completely different way?

any advice gratefully received, thanks :)
Expand|Select|Wrap|Line Numbers
  1. print max([(len(i), i) for i in listOfLists])[1]
Jun 14 '07 #2
r035198x
13,262 8TB
brand new to python so please be gentle! i have a function that returns a list of lists, but now i need to access and print the biggest list. i have started off with this:

for list in listoflists:
>>>size = len(list)

and then i want to do something like compare all the sizes and just print the biggest one, but i can't get my head around the syntax. or should i be going about this a completely different way?

any advice gratefully received, thanks :)
Hi I'm a beginner as well, here's what I did
Expand|Select|Wrap|Line Numbers
  1. >>> a = [2, 4, 6, 6]
  2. >>> b = [7, 8, 9]
  3. >>> c = ["66", "77", "88", "9000"]
  4. >>> d = [b, c, a]
  5. >>> largest = d[0]
  6. for items in d:
  7.     x = len(items)
  8.     y = len(largest)
  9.     if x > y:
  10.         largest = items
  11.  
  12.  
  13. >>> print largest
  14. ['66', '77', '88', '9000']
Jun 14 '07 #3
r035198x
13,262 8TB
Expand|Select|Wrap|Line Numbers
  1. print max([(len(i), i) for i in listOfLists])[1]
Neat. These lists can get ugly at times but once you get it becomes fun.
Jun 14 '07 #4
ghostdog74
511 Expert 256MB
Expand|Select|Wrap|Line Numbers
  1. >>> alist=[[1,2],[4,5,6,7],[1],[5,6,7,8]]
  2. >>> max(alist)
  3. [5, 6, 7, 8]
  4.  
Jun 14 '07 #5
r035198x
13,262 8TB
Expand|Select|Wrap|Line Numbers
  1. >>> alist=[[1,2],[4,5,6,7],[1],[5,6,7,8]]
  2. >>> max(alist)
  3. [5, 6, 7, 8]
  4.  
I did the following

Expand|Select|Wrap|Line Numbers
  1. >>> a = [5, 6, 7]
  2. >>> b = ["6", "6"]
  3. >>> c = [a, b]
  4. >>> max(c)
  5. ['6', '6']
  6. >>>
Does it work only for lists with the same type of data?
Jun 14 '07 #6
bvdet
2,851 Expert Mod 2GB
Expand|Select|Wrap|Line Numbers
  1. >>> alist=[[1,2],[4,5,6,7],[1],[5,6,7,8]]
  2. >>> max(alist)
  3. [5, 6, 7, 8]
  4.  
I think the OP is looking for the longest list.
Expand|Select|Wrap|Line Numbers
  1. >>> listOfLists = [[1,2,3], [3,4], [5,6,7,8], [0,0,0,0,0,0,0,0,0]]
  2. >>> max(listOfLists)
  3. [5, 6, 7, 8]
  4. >>> print max([(len(i), i) for i in listOfLists])[1]
  5. [0, 0, 0, 0, 0, 0, 0, 0, 0]
Jun 14 '07 #7
ghostdog74
511 Expert 256MB
I think the OP is looking for the longest list.
Expand|Select|Wrap|Line Numbers
  1. >>> listOfLists = [[1,2,3], [3,4], [5,6,7,8], [0,0,0,0,0,0,0,0,0]]
  2. >>> max(listOfLists)
  3. [5, 6, 7, 8]
  4. >>> print max([(len(i), i) for i in listOfLists])[1]
  5. [0, 0, 0, 0, 0, 0, 0, 0, 0]
oh my bad for misreading.
Jun 14 '07 #8

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

9 posts views Thread by Dave H | last post: by
41 posts views Thread by Odd-R. | last post: by
3 posts views Thread by s_subbarayan | last post: by
17 posts views Thread by Gal Diskin | last post: by
16 posts views Thread by Michael M. | last post: by
51 posts views Thread by Joerg Schoen | last post: by
3 posts views Thread by =?Utf-8?B?QWRhbSBN?= | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.