Python Forum
Accessing values in list of dictionaries
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Accessing values in list of dictionaries
#1
I'm struggling with iterating over a list of dictionaries to access keys/values based on a user selection.

The first loop works as expected to print the chosen dict values for a user to review but the second one attempting to match the user input to the dictionary key to access the 'id' value within it is not working. The script ends with it printing "It failed."

Is this a data type conversion issue? What am I missing here?

import tmdbsimple as tmdb
tmdb.API_KEY = 'API KEY REMOVED FOR PUBLIC POSTING'

import requests
import locale


search = tmdb.Search()
response = search.movie(query=str(input("Enter a movie name: ")))
#print(response)
list1 = []
inc = 1

#generate list for user to review
for s in search.results:
    print(str(inc) + "." + s['title'] + " " + s['release_date'] + " " + str(s['id']) + " " + "https://image.tmdb.org/t/p/original" + s['poster_path'] + " ")
    list1.append(s['id'])
    inc += 1
    if inc > 5:
        break

enumerate(list1, 1)

choice = input("Choose a movie: ")
choice1 = list1[int(choice)-1]

#iterate over list of dictionaries again to match user choice to correct dict
for t in search.results:
    if t['id'] == choice1:
        print("You chose " + str(choice1))
    else:
        print("It failed.")
        break

#print(list1) prints the expected list of ids
Reply
#2
Only use dictionary[key] if you know the key exists, if you have code in place to catch the KeyError exception, or if it is ok for the call to raise an exception. Take a look at dictionary.get(key).
Reply
#3
Ok I figured out what I did wrong. In testing, I was choosing #2 repeated but that triggered the else block and broke out of the loop before getting to the correct match. *newbie facepalm*
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  sort list of dictionaries by value jacksfrustration 4 264 Jun-21-2024, 08:44 PM
Last Post: deanhystad
  remove duplicates from dicts with list values wardancer84 27 1,191 May-27-2024, 04:54 PM
Last Post: wardancer84
  Sort a list of dictionaries by the only dictionary key Calab 2 782 Apr-29-2024, 04:38 PM
Last Post: Calab
  Copying the order of another list with identical values gohanhango 7 1,405 Nov-29-2023, 09:17 PM
Last Post: Pedroski55
  Search Excel File with a list of values huzzug 4 1,446 Nov-03-2023, 05:35 PM
Last Post: huzzug
  Access list of dictionaries britesc 4 1,275 Jul-26-2023, 05:00 AM
Last Post: Pedroski55
  Comparing List values to get indexes Edward_ 7 1,433 Jun-09-2023, 04:57 PM
Last Post: deanhystad
  Adding values with reduce() function from the list of tuples kinimod 10 3,014 Jan-24-2023, 08:22 AM
Last Post: perfringo
  user input values into list of lists tauros73 3 1,246 Dec-29-2022, 05:54 PM
Last Post: deanhystad
  AttributeError: 'list' object has no attribute 'values' ilknurg 4 15,509 Jan-19-2022, 08:33 AM
Last Post: menator01

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020