0

I have the two list dictionary like this

obj1 = [mydict['obj1'],mydict['obj2'],mydict['obj3'],mydict['obj4']]  
obj2 = [mydict['obj1'],mydict['obj2'],mydict['obj3'],mydict['obj4'], mydict['obj5'] ] 

Now i want that

  1. Count the number of elements in each list
  2. Then based on whichever is greater then get that list of objects

I want a single list which conatins the above two list of(list of) dictionaries based on the higher number of elements so that i cause something like this

mylist = myfunc(objects1, objects2  )

mylist should be a list like [objects1, objects2] depending upon who has greater number of objects.

what is the best way to do that with less lines of code

Something like EDIT

mylist = sorted([obj1, obj2], key=lambda a: len(a), reverse=True)
3
  • I'm afraid I don't understand your first code block at all. Can you write an example that is valid Python code? Commented Apr 26, 2013 at 5:37
  • You should try writing some actual code first. Plus, I think you're confusing "list" with "dictionary." Commented Apr 26, 2013 at 5:40
  • You want less than one line of code? Commented Apr 26, 2013 at 5:49

2 Answers 2

2

There's no need to use a lambda function if it's just going to call a function anyway.

>>> objects1 = [1, 2, 3]
>>> objects2 = ['1', '2', '3', '4']
>>> 
>>> mylist = [objects1, objects2]
>>> max(mylist, key=len)
['1', '2', '3', '4']

>>> sorted(mylist, key=len, reverse=True)
[['1', '2', '3', '4'], [1, 2, 3]]
Sign up to request clarification or add additional context in comments.

Comments

0
objects1 = [1, 2, 3]
objects2 = ['1', '2', '3', '4']

mylist = [objects1, objects2]
mylist.sort(key=len, reverse=True)
print mylist

[['1', '2', '3', '4'], [1, 2, 3]]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.