Is there a better way to convert a list to a dictionary in Python with keys but no values?

I was sure that there would be one liner for converting the list into a dictionary, where the elements in the list are keys and the dictionary has no meaning.

The only way I could do this was to object.

"Using lists when the result is ignored is misleading and inefficient. Better for loop

 myList = ['a','b','c','d'] myDict = {} x=[myDict.update({item:None}) for item in myList] >>> myDict {'a': None, 'c': None, 'b': None, 'd': None} 

This works, but is there a better way to do this?

+6
python dictionary list list-comprehension
source share
6 answers

Use dict.fromkeys :

 >>> my_list = [1, 2, 3] >>> dict.fromkeys(my_list) {1: None, 2: None, 3: None} 

The default value is None , but you can specify them as an optional argument:

 >>> my_list = [1, 2, 3] >>> dict.fromkeys(my_list, 0) {1: 0, 2: 0, 3: 0} 

From the docs:

a.fromkeys (seq [, value]) Creates a new dictionary with the keys to seq and the values ​​are set to value.

dict.fromkeys is a class method that returns a new dictionary. The default value is None. New in version 2.3.

+22
source share

You can use set instead of dict:

 >>> myList=['a','b','c','d'] >>> set(myList) set(['a', 'c', 'b', 'd']) 

This is best if you never need to store values ​​and just store an unordered set of unique elements.

+15
source share

To answer the initial problems associated with query execution (for searching in dict vs set ), it is somewhat surprising that finding a dict can be very fast (in Python 2.5.1 on my rather slow laptop), assuming, for example, that half of the search queries fails and half success. Here, as it turns out:

 $ python -mtimeit -s'k=dict.fromkeys(range(99))' '5 in k and 112 in k' 1000000 loops, best of 3: 0.236 usec per loop $ python -mtimeit -s'k=set(range(99))' '5 in k and 112 in k' 1000000 loops, best of 3: 0.265 usec per loop 

performing each check several times to make sure they are repeatable. Thus, if these 30 nanoseconds or less on a slow laptop are in an extremely important bottleneck, it might be worth going to the dict.fromkeys obscure solution rather than a simple, obvious, readable, and clearly correct set (unusual - almost always in Python is simple and a direct solution has performance advantages too).

Of course, you need to check with one native version of Python, the machine, the data and the ratio of successful tests with errors, and confirm with extremely accurate profiling that shaving 30 nanoseconds (or something else) this search will be important.

Fortunately, in the vast majority of cases, this will prove completely unnecessary ... but since programmers will be obsessed with pointless micro-optimizations, no matter how many times they are told about their irrelevance, the timeit module is right there in the standard library to do these basically pointless micro tests as light as pie anyway! -)

+5
source share

And here's a pretty wrong and inefficient way to do this with a map:

 >>> d = dict() >>> map (lambda x: d.__setitem__(x, None), [1,2,3]) [None, None, None] >>> d {1: None, 2: None, 3: None} 
+1
source share

You can use list comprehension:

 my_list = ['a','b','c','d'] my_dict = dict([(ele, None) for ele in my_list]) 
+1
source share

Perhaps you can use itertools:

 >>>import itertools >>>my_list = ['a','b','c','d'] >>>d = {} >>>for x in itertools.imap(d.setdefault, my_list): pass >>>print d {'a': None, 'c': None, 'b': None, 'd': None} 

For huge lists, maybe this is very good: P

+1
source share

All Articles