This will convert the dict_keys object to a list:
list(newdict.keys())
On the other hand, you should ask yourself whether or not it matters. It is Pythonic to assume duck typing -- if it looks like a duck and it quacks like a duck, it is a duck. The dict_keys object can be iterated over just like a list. For instance:
for key in newdict.keys():
print(key)
Note that dict_keys doesn't support insertion newdict[k] = v, though you may not need it.
How do I return dictionary keys as a list in Python? - Stack Overflow
Python dictionary keys "new" syntax
What is a "key" in Python dictionaries?
Printing Value of Dictionary As Int
This will convert the dict_keys object to a list:
list(newdict.keys())
On the other hand, you should ask yourself whether or not it matters. It is Pythonic to assume duck typing -- if it looks like a duck and it quacks like a duck, it is a duck. The dict_keys object can be iterated over just like a list. For instance:
for key in newdict.keys():
print(key)
Note that dict_keys doesn't support insertion newdict[k] = v, though you may not need it.
Python >= 3.5 alternative: unpack into a list literal [*newdict]
New unpacking generalizations (PEP 448) were introduced with Python 3.5 allowing you to now easily do:
>>> newdict = {1:0, 2:0, 3:0}
>>> [*newdict]
[1, 2, 3]
Unpacking with * works with any object that is iterable and, since dictionaries return their keys when iterated through, you can easily create a list by using it within a list literal.
Adding .keys() i.e [*newdict.keys()] might help in making your intent a bit more explicit though it will cost you a function look-up and invocation. (which, in all honesty, isn't something you should really be worried about).
The *iterable syntax is similar to doing list(iterable) and its behaviour was initially documented in the Calls section of the Python Reference manual. With PEP 448 the restriction on where *iterable could appear was loosened allowing it to also be placed in list, set and tuple literals, the reference manual on Expression lists was also updated to state this.
Though equivalent to list(newdict) with the difference that it's faster (at least for small dictionaries) because no function call is actually performed:
%timeit [*newdict]
1000000 loops, best of 3: 249 ns per loop
%timeit list(newdict)
1000000 loops, best of 3: 508 ns per loop
%timeit [k for k in newdict]
1000000 loops, best of 3: 574 ns per loop
with larger dictionaries the speed is pretty much the same (the overhead of iterating through a large collection trumps the small cost of a function call).
In a similar fashion, you can create tuples and sets of dictionary keys:
>>> *newdict,
(1, 2, 3)
>>> {*newdict}
{1, 2, 3}
beware of the trailing comma in the tuple case!
hey
just found out you can use tuples as dictionary keys which saved me doing a lot of weird workarounds.
any other useful hidden gems in pythons syntax?
thanks!
Edit: Solved!
menu = {
'1) empty ': '$3.25',
'1) eggs ': '$3.25',
'2) bacon ': '$4.00',
'3) pancakes ': '$2.50',
'4) orange juice ': '$1.25',
'5) oatmeal ': '$3.99',
'6) milk ': '$1.25',
'7) donut ': '$2.00',
}
def get_price_of_menu_choice(x):
value = list(menu.values())[x] ## lookup item to get price
return(value) ## return price
Edit: How can i make the prices print as numbers without the quote marks??? This is part of a homework assignment but the course never covered dictionaries. The assignments asks you to print a menu in a specifically formatted way. Then lookup the item to return a price as a number ( float or integer). But currently it is printing with quote marks.