0

I have a list. I want to use its elements to initialise a dictionary, which n pairs of key:list().

Current input :

names = ['John', 'Jane', 'Mark']

desired output after transformation :

names_dict = {'John': list(), 'Jane': list(), 'Mark': list()}

The easiest way I see is appending iteratively the key:list() pairs in a for loop. Are there any more pythonic ways of doing this transformation ?

Louis GRIMALDI
  • 101
  • 1
  • 12

1 Answers1

1

A dict comprehension is not much different than a for loop but slightly more pythonic:

names_dict = {name: list() for name in names}
Selcuk
  • 57,004
  • 12
  • 102
  • 110