I started learning Python few weeks ago (with no previous knowledge of it nor programming). I want to create a definition which will for given dictionary as an argument return a tuple consisted of two lists - one where there are only keys of dictionary, and the other where there are only values of the given dictionary. Basically the code will look like this:
"""Iterate over the dictionary named letters, and populate the two lists so that
keys contains all the keys of the dictionary, and values contains
all the corresponding values of the dictionary. Return this as a tuple in the end."""
def run(dict):
keys = []
values = []
for key in dict.keys():
keys.append(key)
for value in dict.values():
values.append(value)
return (keys, values)
print run({"a": 1, "b": 2, "c": 3, "d": 4})
This code worked perfectly (it's not my solution though). But what if I do not want to use the .keys() and .values() methods? In that case, I tried using something like this, but I got "unhashable type: 'list'" error message:
def run(dict):
keys = []
values = []
for key in dict:
keys.append(key)
values.append(dict[keys])
return (keys, values)
print run({"a": 1, "b": 2, "c": 3, "d": 4})
What seems to be the problem?