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
  Copying the order of another list with identical values gohanhango 7 1,062 Nov-29-2023, 09:17 PM
Last Post: Pedroski55
  Search Excel File with a list of values huzzug 4 1,147 Nov-03-2023, 05:35 PM
Last Post: huzzug
  Sort a list of dictionaries by the only dictionary key Calab 1 452 Oct-27-2023, 03:03 PM
Last Post: buran
  Access list of dictionaries britesc 4 1,028 Jul-26-2023, 05:00 AM
Last Post: Pedroski55
  Comparing List values to get indexes Edward_ 7 1,083 Jun-09-2023, 04:57 PM
Last Post: deanhystad
  Adding values with reduce() function from the list of tuples kinimod 10 2,513 Jan-24-2023, 08:22 AM
Last Post: perfringo
  user input values into list of lists tauros73 3 1,025 Dec-29-2022, 05:54 PM
Last Post: deanhystad
  AttributeError: 'list' object has no attribute 'values' ilknurg 4 14,779 Jan-19-2022, 08:33 AM
Last Post: menator01
  Need to parse a list of boolean columns inside a list and return true values Python84 4 2,036 Jan-09-2022, 02:39 AM
Last Post: Python84
  List of dataframe values beginning with x,y or z glidecode 3 1,891 Nov-08-2021, 10:16 PM
Last Post: glidecode

Forum Jump:

User Panel Messages

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