How can I print multiple indexes of a certain value in a dictionary?

Question:

I’m just learning python and I have a problem. how can I print multiple indexes of a certain value in a dictionary? In particular, I want to print the index of each element of the dictionary_title array which has gender_ids as the key.

dictionary_title={
{'label': 'Green', 'genre_ids': 878},
{'label': 'Pink', 'genre_ids': 16},
{'label': 'Orange', 'genre_ids': 28},
{'label': 'Yellow', 'genre_ids': 9648},
{'label': 'Red', 'genre_ids': 878},
{'label': 'Brown', 'genre_ids': 12},
{'label': 'Black', 'genre_ids': 28},
{'label': 'White', 'genre_ids': 14},
{'label': 'Blue', 'genre_ids': 28},
{'label': 'Light Blue', 'genre_ids': 10751},
{'label': 'Magenta', 'genre_ids': 28},
{'label': 'Gray', 'genre_ids': 28}}

This is my code:

   for values in dictionary_title["genre_ids"]: 
   for item in values:       
       if item == 28:      
           print(values.index(item))  

For example, I want to print index:2,6,8,10,11 which are the indexes of the items with the key, genre_ids=28. How can I do it?

Asked By: Elly

||

Answers:

You can use a list comprehension with enumerate.

dictionary_title=[
{'label': 'Green', 'genre_ids': 878},
{'label': 'Pink', 'genre_ids': 16},
{'label': 'Orange', 'genre_ids': 28},
{'label': 'Yellow', 'genre_ids': 9648},
{'label': 'Red', 'genre_ids': 878},
{'label': 'Brown', 'genre_ids': 12},
{'label': 'Black', 'genre_ids': 28},
{'label': 'White', 'genre_ids': 14},
{'label': 'Blue', 'genre_ids': 28},
{'label': 'Light Blue', 'genre_ids': 10751},
{'label': 'Magenta', 'genre_ids': 28},
{'label': 'Gray', 'genre_ids': 28}]

res = [i for i, o in enumerate(dictionary_title) if o['genre_ids'] == 28]
print(res)
Answered By: Unmitigated

"dictionary title" must be a list!!! I leave you the code of how to do it 🙂

dictionary_title=[
{'label': 'Green', 'genre_ids': 878},
{'label': 'Pink', 'genre_ids': 16},
{'label': 'Orange', 'genre_ids': 28},
{'label': 'Yellow', 'genre_ids': 9648},
{'label': 'Red', 'genre_ids': 878},
{'label': 'Brown', 'genre_ids': 12},
{'label': 'Black', 'genre_ids': 28},
{'label': 'White', 'genre_ids': 14},
{'label': 'Blue', 'genre_ids': 28},
{'label': 'Light Blue', 'genre_ids': 10751},
{'label': 'Magenta', 'genre_ids': 28},
{'label': 'Gray', 'genre_ids': 28}]

for idx in range(0, len(dictionary_title)):
    if dictionary_title[idx]["genre_ids"] == 28:
        print(idx)

You can also do it using different loop styles.

Style 2: Loop using enumerate()

for enumerate_item in enumerate(dictionary_title):
    if enumerate_item[1]["genre_ids"] == 28:
        print(enumerate_item[0])

Style 3: Loop using enumerate() just in other way…

for idx, dict in enumerate(dictionary_title):
    if dict['genre_ids'] == 28:
        print(idx)

Style 4: Loop using list comprehension

rest = [enumerate_item[0] for enumerate_item in enumerate(dictionary_title) if enumerate_item[1]['genre_ids'] == 28]
print(rest)

Style 5: Loop using list comprehension just in other way…

rest = [(idx) for idx, dict in enumerate(dictionary_title) if dict['genre_ids'] == 28]
print(rest)
Answered By: Jumi
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.