r/pythonhelp • u/[deleted] • Dec 07 '22
trying to get value from a dict with integer key
I am working with a dict which is structured like this inside a function:
listOfInformation = [{123456789: {'PokemonId': '123456789', 'PokemonName': 'Pikachu', 'PokemonAttack': 'thunderbolt'}}]In that function, I'm passing an integer as an argument (pokemon_id) and then trying to test if the key-value pair exists like this:
listOfInformation[pokemon_id]But I am getting an error of IndexError, list index out of range. I can't figure out why would I get this error. how can I fix this?
2
Upvotes
1
u/Goobyalus Dec 07 '22
listOfInformationis a list of dictionaries. It is not a dictionary or a function.listOfInformation(pokemon_id)attempts to calllistOfInformationas though it were a function, which will TypeError.listOfInformation[pokemon_id]will attempt to index the list at elementpokemon_id, which if it's123456789, is out of bounds of the single-element list.I think you meant to define something like
and access it like