Reputation: 313
I have the dictionary:
dict1 = {'A': {'val1': '5', 'val2': '1'},
'B': {'val1': '10', 'val2': '10'},
'C': {'val1': '15', 'val3': '100'}
Here, I have one key, with two values. I can obtain the key by using:
letters = dict1.keys()
which returns:
['A', 'B', 'C']
I am use to working with arrays and being able to "slice" them. How can I break this dictionary in a similar way as the key for the values?
val1_and_val2 = dict1.values()
returns:
[{'val1': '5', 'val2': '1'},
{'val1': '10', 'val2': '10'},
{'val1': '15', 'val2': '100'}]
How can I get:
number1 = [5, 10, 15]
number2 = [1, 10, 100]
Upvotes: 0
Views: 1234
Reputation: 4942
If I understand you correctly then:
number1 = [val["val1"] for val in dict1.values()]
If you prefer, this will accomplish the same thing with lambdas.
number1 = map(lambda value: value["val1"], dict1.values())
Note how you really need to take the dict1[key]["val1"] to get an individual value.
Upvotes: 1