i have a list of dicts
JavaScript
x
4
1
[{"id":1, "name": "jane", "contact": {"email": "jane@gmail.com", "phone": "8179482938"}},
2
{"id":2, "name": "john", "contact": {"email": "john@gmail.com", "phone": "8179482937"}},
3
{"id":3, "name": "june", "contact": {"email": "june@gmail.com", "phone": "8179482936"}}]
4
how can i find the index of jane given only her phone number (8179482938)?
Advertisement
Answer
To get an index, you can do (lst
is your list from question):
JavaScript
1
3
1
idx = next(i for i, d in enumerate(lst) if d["contact"]["phone"] == "8179482938")
2
print(idx)
3
Prints:
JavaScript
1
2
1
0
2