I was sure that there would be a one liner to convert a list to a dictionary where the items in the list were keys and the dictionary had no values.
The only way I could find to do it was argued against.
“Using list comprehensions when the result is ignored is misleading and inefficient. A for loop is better”
myList = ['a','b','c','d']
myDict = {}
x=[myDict.update({item:None}) for item in myList]
>>> myDict
{'a': None, 'c': None, 'b': None, 'd': None}
It works, but is there a better way to do this?
Use
dict.fromkeys:Values default to
None, but you can specify them as an optional argument:From the docs: