I have a sample code here containing a list of dicts,
[{'uri': 1, 'name': 'one'}, {'uri': 2, 'name': 'two'}, {'uri': 3, 'name': 'three'}]
The output I expect is the value from the key uri
[1, 2, 3]
I have a sample code here containing a list of dicts,
[{'uri': 1, 'name': 'one'}, {'uri': 2, 'name': 'two'}, {'uri': 3, 'name': 'three'}]
The output I expect is the value from the key uri
[1, 2, 3]
A list comprehension should do the trick:
myDict = [{'uri': 1, 'name': 'one'}, {'uri': 2, 'name': 'two'}, {'uri': 3, 'name': 'three'}]
result = [d['uri'] for d in myDict]
A list comprehension is the most readable way to do this:
[item['uri'] for item in my_dictionary]
There are several ways to do that. I like using python‘s list comprehension which is something you will find very useful.
my_original_list = [{'uri': 1, 'name': 'one'}, {'uri': 2, 'name': 'two'}, {'uri': 3, 'name': 'three'}]
my_list = [elem["uri"] for elem in my_original_list]