1

Like let's say my list is

["Hello","My","Name","is","python"]

Can we make this like

{"Hello":5,"My":2,"Name":4,"is":2,"python":6}

I'm trying to make a programming challenge and finding the words. But to faster the procces if it finds all lengths so it can check if it is like:
"I am 5 and it is 6 so it can't be me"

Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
Dr. UK
  • 73
  • 1
  • 2
  • 7

1 Answers1

6

Use a dictionary comprehension along with len function. See this for more information about dict-comps.

>>> {i:len(i) for i in ["Hello","My","Name","is","python"]}
{'Name': 4, 'python': 6, 'My': 2, 'Hello': 5, 'is': 2}

Some fun with builtins in Python2, You can do

>>> l = ["Hello","My","Name","is","python"]
>>> dict(zip(l,map(len,l)))
{'Name': 4, 'python': 6, 'My': 2, 'Hello': 5, 'is': 2}
Community
  • 1
  • 1
Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140