Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I was looking for how to create nested list from dictionary. Here is the given Dictionary.

Dict={'Jason Seifer': ['Ruby Foundations', 'Ruby on Rails Forms', 'Technology Foundations'], 'Kenneth Love': ['Python Basics', 'Python Collections'],'Jason Bourne':['black berry','mango','potato','oli','key'],'fruits':['cherry','pineapple','banana','coconut']}

Excepted output:

[['Jason Bourne', 5], ['Kenneth Love', 2], ['fruits', 4], ['Jason Seifer', 3]]

Here order doesn't matter

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
191 views
Welcome To Ask or Share your Answers For Others

1 Answer

With a simple comprehension:

result = [[k, len(v)] for k,v in Dict.items()]

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...