-2

This is my json :

{'1': {'name': 'poulami', 'password': 'paul123', 'profession': 'user', 'uid': 'poulamipaul'}, '2': {'name': 'test', 'password': 'testing', 'profession': 'tester', 'uid': 'jarvistester'}}

I want to get a list of all the values of name.
What should be my code in python

2 Answers2

1

d.values gives all the values, then you can get the attribute name of each value.

d = {'1': {'name': 'poulami', 'password': 'paul123', 'profession': 'user', 'uid': 'poulamipaul'}, '2': {'name': 'test', 'password': 'testing', 'profession': 'tester', 'uid': 'jarvistester'}}

[i['name'] for i in d.values()]
['poulami', 'test']

Also note that d.values returns a generator and not a list so to convert to list use list(d.values())

AmaanK
  • 1,032
  • 5
  • 25
Epsi95
  • 8,832
  • 1
  • 16
  • 34
0

That is not JSON format. It is a Python Dictionary.

Iterate over the values of the dictionary(d.values()) and get the name from each item.

d = {'1': {'name': 'poulami', 'password': 'paul123', 'profession': 'user', 'uid': 'poulamipaul'}, '2': {'name': 'test', 'password': 'testing', 'profession': 'tester', 'uid': 'jarvistester'}}

names_list = []

for i in d.values():
    names_list.append(i['name'])
names_list = ['poulami', 'test']
Ram
  • 4,724
  • 2
  • 14
  • 22