'how to get a key and values and combine in list

data = [{"name":"Anne", "followers":["Brian"]}, {"name":"Cindy", "followers":["Brian","G osh","Anne"]},{"name":"Dave", "followers":[]}]

output : [{"name": ["Brian"] , "follows":["Anne","Cindy"]},...] etc...

my code from now :

from operator import itemgetter

data = data = [{"name":"Anne", "followers":["Brian"]}, {"name":"Cindy", "followers":["Brian","Gosh","Anne"]},{"name":"Dave", "followers":[]}]

x = list(map(itemgetter('followers'), data)) y = list(map(itemgetter('name') ,data))

print("name : " + str(x), " follow : " + str(y))

how to get combine same values and get from key ? i think this is a list in dict ...sorry if mistake . i am newbie using this language



Solution 1:[1]

It looks like there's a couple errors in your code. First off, in the place where you're defining x and y, it needs to be on separate lines. In other words, change

x = list(map(itemgetter('followers'), data)) y = list(map(itemgetter('name') ,data))

to

x = list(map(itemgetter('followers'), data))

y = list(map(itemgetter('name') ,data))

to be on separate lines. Also, since that is the only error in your code it looks like, what is it that you are trying to achieve with this code? Since when I run it, all it outputs is name : [['Brian'], ['Brian', 'Gosh', 'Anne'], []] follow : ['Anne', 'Cindy', 'Dave']. Other than that, good luck with your programming adventures!

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Brennan Reinhard