given a dictionary:
entries = {'blue': ' the color of the sky', 'coffee': ' fuel for programmers'}
how do I append the keys into a list?
I got this far:
results = []
for entry in entries:
results.append(entry[?])
given a dictionary:
entries = {'blue': ' the color of the sky', 'coffee': ' fuel for programmers'}
how do I append the keys into a list?
I got this far:
results = []
for entry in entries:
results.append(entry[?])
You can access the keys of a dictionary with .keys(). You can turn that into a list with list(). For example,
entries = {'blue': ' the color of the sky', 'coffee': ' fuel for programmers'}
keys = list(entries.keys())
The keys of the dict is already a list like object. If you really want a list, its easy to convert
>>> entries = {'blue': ' the color of the sky', 'coffee': ' fuel for programmers'}
>>> l = list(entries)
>>> l
['blue', 'coffee']
If you want to add the keys to an existing list
>>> mylist = [1,2,3]
>>> mylist += entries
>>> mylist
[1, 2, 3, 'blue', 'coffee']
You can frequently just use the dict object
>>> entries.keys()
dict_keys(['blue', 'coffee'])
>>> 'blue' in entries
True
>>> for key in entries:
... print(key)
...
blue
coffee