0

I have list as this :

[{"id": 101, "name": "music1"}, {"id": 102, "name": "music2"}]

how do I get a id of object matching with specific name:

for e.g,

input: music2
output : 102
usr_11
  • 548
  • 10
  • 31
  • 3
    ... Have you tried anything? Iterating over the list, checking the `'name'`, and getting the corresponding `'id'`? What exactly do you not know how to do? – juanpa.arrivillaga Mar 07 '18 at 07:24
  • If you are going to need to query this data a lot, why not just convert the entire thing to a dict or dicts? – cs95 Mar 07 '18 at 07:24
  • @juanpa.arrivillaga was trying to find out if its possible without iterating a loop. – usr_11 Mar 07 '18 at 07:31
  • No, it isn't, it will require linear search of your list. To preform better, use a different data-structure. – juanpa.arrivillaga Mar 07 '18 at 07:32

1 Answers1

0
a = [{"id": 101, "name": "music1"}, {"id": 102, "name": "music2"}]

input = "music2"
for i in a:
    if i["name"] == input:
        print(i["id"])
Rakesh
  • 81,458
  • 17
  • 76
  • 113