# Basic syntax:
list_of_keys = list(dictionary.keys())
# This is our example dictionary
petAges = {"Cat": 4, "Dog": 2, "Fish": 1, "Parrot": 5}
# This will be our list, epmty for now
petAgesList = []
# Search through the dictionary and find all the keys and values
for key, value in petAges.items():
petAgesList.append([key, value]) # Add the key and value to the list
print(petAgesList) # Print the now-filled list
# Output: [['Cat', 4], ['Dog', 2], ['Fish', 1], ['Parrot', 5]]
for key, value in dict.iteritems():
temp = [key,value]
dictlist.append(temp)
>> d = {'a': 'Arthur', 'b': 'Belling'}
>> d.items()
[('a', 'Arthur'), ('b', 'Belling')]
>> d.keys()
['a', 'b']
>> d.values()
['Arthur', 'Belling']
# To get all the keys of a dictionary as a list, see below
newdict = {1:0, 2:0, 3:0}
list(newdict)
# Output:
# [1, 2, 3]
>>> newdict = {1:0, 2:0, 3:0}
>>> [*newdict]
[1, 2, 3]
"""
In [1]: newdict = {1:0, 2:0, 3:0}
In [2]: %timeit [*newdict]
107 ns ± 5.42 ns per loop
In [3]: %timeit list(newdict)
144 ns ± 7.99 ns per loop
In [4]: %timeit [k for k in newdict]
257 ns ± 21.6 ns per loop
"""
>>> list(newdict.keys()) # don't do this.
newlist = list()
for i in newdict.keys():
newlist.append(i)
list(newdict.keys())