Like this:
Copykeys = ['a', 'b', 'c']
values = [1, 2, 3]
dictionary = dict(zip(keys, values))
print(dictionary) # {'a': 1, 'b': 2, 'c': 3}
Voila :-) The pairwise dict constructor and zip function are awesomely useful.
Like this:
Copykeys = ['a', 'b', 'c']
values = [1, 2, 3]
dictionary = dict(zip(keys, values))
print(dictionary) # {'a': 1, 'b': 2, 'c': 3}
Voila :-) The pairwise dict constructor and zip function are awesomely useful.
Imagine that you have:
Copykeys = ('name', 'age', 'food') values = ('Monty', 42, 'spam')What is the simplest way to produce the following dictionary ?
Copydict = {'name' : 'Monty', 'age' : 42, 'food' : 'spam'}
Most performant, dict constructor with zip
Copynew_dict = dict(zip(keys, values))
In Python 3, zip now returns a lazy iterator, and this is now the most performant approach.
dict(zip(keys, values)) does require the one-time global lookup each for dict and zip, but it doesn't form any unnecessary intermediate data-structures or have to deal with local lookups in function application.
Runner-up, dict comprehension:
A close runner-up to using the dict constructor is to use the native syntax of a dict comprehension (not a list comprehension, as others have mistakenly put it):
Copynew_dict = {k: v for k, v in zip(keys, values)}
Choose this when you need to map or filter based on the keys or value.
In Python 2, zip returns a list, to avoid creating an unnecessary list, use izip instead (aliased to zip can reduce code changes when you move to Python 3).
Copyfrom itertools import izip as zip
So that is still (2.7):
Copynew_dict = {k: v for k, v in zip(keys, values)}
Python 2, ideal for <= 2.6
izip from itertools becomes zip in Python 3. izip is better than zip for Python 2 (because it avoids the unnecessary list creation), and ideal for 2.6 or below:
Copyfrom itertools import izip
new_dict = dict(izip(keys, values))
Result for all cases:
In all cases:
Copy>>> new_dict
{'age': 42, 'name': 'Monty', 'food': 'spam'}
Explanation:
If we look at the help on dict we see that it takes a variety of forms of arguments:
>>> help(dict)
class dict(object)
| dict() -> new empty dictionary
| dict(mapping) -> new dictionary initialized from a mapping object's
| (key, value) pairs
| dict(iterable) -> new dictionary initialized as if via:
| d = {}
| for k, v in iterable:
| d[k] = v
| dict(**kwargs) -> new dictionary initialized with the name=value pairs
| in the keyword argument list. For example: dict(one=1, two=2)
The optimal approach is to use an iterable while avoiding creating unnecessary data structures. In Python 2, zip creates an unnecessary list:
Copy>>> zip(keys, values)
[('name', 'Monty'), ('age', 42), ('food', 'spam')]
In Python 3, the equivalent would be:
Copy>>> list(zip(keys, values))
[('name', 'Monty'), ('age', 42), ('food', 'spam')]
and Python 3's zip merely creates an iterable object:
Copy>>> zip(keys, values)
<zip object at 0x7f0e2ad029c8>
Since we want to avoid creating unnecessary data structures, we usually want to avoid Python 2's zip (since it creates an unnecessary list).
Less performant alternatives:
This is a generator expression being passed to the dict constructor:
Copygenerator_expression = ((k, v) for k, v in zip(keys, values))
dict(generator_expression)
or equivalently:
Copydict((k, v) for k, v in zip(keys, values))
And this is a list comprehension being passed to the dict constructor:
Copydict([(k, v) for k, v in zip(keys, values)])
In the first two cases, an extra layer of non-operative (thus unnecessary) computation is placed over the zip iterable, and in the case of the list comprehension, an extra list is unnecessarily created. I would expect all of them to be less performant, and certainly not more-so.
Performance review:
In 64 bit Python 3.8.2 provided by Nix, on Ubuntu 16.04, ordered from fastest to slowest:
Copy>>> min(timeit.repeat(lambda: dict(zip(keys, values))))
0.6695233230129816
>>> min(timeit.repeat(lambda: {k: v for k, v in zip(keys, values)}))
0.6941362579818815
>>> min(timeit.repeat(lambda: {keys[i]: values[i] for i in range(len(keys))}))
0.8782548159942962
>>>
>>> min(timeit.repeat(lambda: dict([(k, v) for k, v in zip(keys, values)])))
1.077607496001292
>>> min(timeit.repeat(lambda: dict((k, v) for k, v in zip(keys, values))))
1.1840861019445583
dict(zip(keys, values)) wins even with small sets of keys and values, but for larger sets, the differences in performance will become greater.
A commenter said:
minseems like a bad way to compare performance. Surelymeanand/ormaxwould be much more useful indicators for real usage.
We use min because these algorithms are deterministic. We want to know the performance of the algorithms under the best conditions possible.
If the operating system hangs for any reason, it has nothing to do with what we're trying to compare, so we need to exclude those kinds of results from our analysis.
If we used mean, those kinds of events would skew our results greatly, and if we used max we will only get the most extreme result - the one most likely affected by such an event.
A commenter also says:
In python 3.6.8, using mean values, the dict comprehension is indeed still faster, by about 30% for these small lists. For larger lists (10k random numbers), the
dictcall is about 10% faster.
I presume we mean dict(zip(... with 10k random numbers. That does sound like a fairly unusual use case. It does makes sense that the most direct calls would dominate in large datasets, and I wouldn't be surprised if OS hangs are dominating given how long it would take to run that test, further skewing your numbers. And if you use mean or max I would consider your results meaningless.
Let's use a more realistic size on our top examples:
Copyimport numpy
import timeit
l1 = list(numpy.random.random(100))
l2 = list(numpy.random.random(100))
And we see here that dict(zip(... does indeed run faster for larger datasets by about 20%.
Copy>>> min(timeit.repeat(lambda: {k: v for k, v in zip(l1, l2)}))
9.698965263989521
>>> min(timeit.repeat(lambda: dict(zip(l1, l2))))
7.9965161079890095
How to turn a list of lists into a dictionary where the first item of each sublist is used as key?
Seeking to populate a dictionary from a series of lists
how to know when to use Lists and when to Use dictionaries?
This is a complex question because there are times when either a dictionary, list, or tuple will work; and there are times where only one or two would work.
Dict
A dict does not* maintain order. Think of it like a literal dictionary: if I want to know the value of something, I just have to know its key. It's location inside the dict is completely irrelevant as long as I know the key. This makes it great for retrieving things that you can give a logical name to and updating things that you can give a logical name to. It's extremely fast for looking up the value of something. similar to a real dictionary, you have a good idea of where to start "searching" in an actual dictionary to find a word and don't have to flip through each and every page of the dictionary to find what you're looking for.
List
A list maintains order. If I load values into a list, they're always guaranteed to be in that order forever unless I explicitly change it. This isn't the case with dicts*. This allows you to do things like sort and order data in meaningful ways. A draw back with a list is that finding out if a value exists in a list could take a very long time if the items are unordered.
If I have an unordered list like this:
[['Hannah', 'black'], ['Mark', 'brown'], ['Avery', 'blonde'],....]
And I want to lookup the hair color of 'Peter', I have to search through every single value in the list to see if it matches Peter. If 'Peter' doesn't exist in the list, I will have gone through every value in the list. If the list of thousands of names long, this will take thousands of operations. However, if I had a dict mapping name to hair color, looking up if Peter exists in the dict would take roughly 1 operation, even if the dict was millions of mappings long.
Tuple
Tuples a bit like immutable lists. Meaning they hold values, and maintain order, but once you create one, you can never modify/add/or delete any of its values. A good use case of a tuple are related data that comes in fixed sizes and the order has a specific meaning:
# rgb color values rgb = (255, 120, 30) # x, y coordinates coordinate = (-1, 0)
Tuples are also great for what I think of as "throw-away-lists". A list that you only use once, that will never grow, shrink, or change values can probably be turned into a tuple.
for name in ['Bob', 'Peter', 'Sarah']:
print(name)
for name in ('Bob', 'Peter', 'Sarah'):
print(name)In the example above, both have the exact same output, although you will see people use either one. The list of names is never growing, shrinking, or changing value, so it can be converted to a tuple and probably should be converted to a tuple because a tuple is more efficient than a list.
-------------------------------------------------------------
In your example both dicts and lists would work, so why did the Automate The Boarding Stuff book use a dict for the Tic Tac Toe board instead of a list? (1) Probably because dict is a little harder to grasp so it would be good practice for the reader to get more exposure to using a dict. (2) If we did the list implementation, you would have to put the board in some type of order. Like this:
['', 'X', 'O', '', 'X', '', 'X', 'O', 'O']
One problem with the list implementation is that it is not obvious which index corresponds to which square on a Tic Tac Toe board. Are the first 3 indexes representing the top row? Or do they represent the left column? There's no way to really telling from the code alone. With the dict implementation, it's extremely obvious which value corresponds to which square on a Tic Tac Toe board.
But in this case, yes either a list or dict would work. A tuple would probably not be a great idea because it's immutable, and you can't update the values it holds without creating a brand new tuple.
-------------------------------------------------------------
* later version of Python does maintain dict entry order
More on reddit.comHow to create a nested dictionary taking the keys from a list?
Videos
The original list I have is:
[['James', '100.00', '90.00', '85.50'], ['Nick', '78.00', '85.00', '80.50'], ['William', '95.50', '92.00', '100.00']]
I want to turn the list into a dictionary that look like this:
{'James': ['100.00', '90.00', '85.50'], 'Nick': ['78.00', '85.00', '80.50'], 'William': ['95.50', '92.00', '100.00']}
I am a beginner of python coding, could anyone please tell me how to get the output for this?