python

Get all values by key name from a list of dictionaries Python

If you want to get all values by using the key name from a list of dictionaries then you can use the below code for that.

users = [
    { "name": "John Deo", "username": "john01" },
    { "name": "Stephen Hawk", "username": "stephen" },
    { "name": "Rick Grimes", "username": "rick21" }
]

names = [d["name"] for d in fruits] #returns list
print(names)
#prints - ["John Deo", "Stephen Hawk", "Rick Grimes"]


names_comma_seperated = ','.join( [d["title"] for d in data['current']] )
print(names_comma_seperated)
#prints - John Deo,Stephen Hawk,Rick Grimes
Was this helpful?