Home > Software engineering >  list of dictionaries . how to extract specific data in a list
list of dictionaries . how to extract specific data in a list

Time:12-12

my data Hey! I have list of dictonary i want to retrive the name and genre in separate lists how can I do this.I have tried many methods but i am unable to do anything can anyone help I am new to python.

names=[]
genres=[]
for i in list_of_val:
    names=list_of_val['name']
    genres=list_of_val['genre']

enter image description here

CodePudding user response:

I think you got a dictionary with one of names or genre as key and other as value. let say u have a dictionary d and you want to separate out keys and values:

names=[]
genre=[]
for i in list_of_dictionaries:
    for z in i:
        if z=='name':
         names.append(i[z])
        if z=='genre':
         genre.append(i[z])

This will give you the corresponding lists, please comment if you meant something else because from your question it seems like this

CodePudding user response:

i have figured this out

names=[]
genres=[]
for i in list_of_val:
    genres.append(i['genre'])
    names.append(i['name'])
  • Related