I'm getting data from PokeAPI but if the atribute has more than one value like abilities
and stats
I get only the last one.
dic_abi = {} #logic for abilities
for i in lista['abilities']:
dic_abi = i['ability']['name']
contexto = {
#"pokemons": dic,
'ID': lista['id'],
'Nome': lista['name'],
'Tipo': lista['types'],
'Peso': lista['weight'],
'Altura': lista['height'],
'Habilidades': dic_abi, #more than one value
'Estatisticas': lista['stats'] #more than one value
}
JSON that I'm trying to get:
{"abilities":[{"ability":{"name":"static","url":"https://pokeapi.co/api/v2/ability/9/"},"is_hidden":false,"slot":1},{"ability":{"name":"lightning-rod" ........
It's hard to understand what you're trying to achieve. If you want to make a list of abilities' names in lista['abilities']
, then you could do list comprehension instead of a for loop
dic_abi = [i['ability']['name'] for i in lista['abilities']]
If instead you want to create a dict with abilities' names as key
and the abilities as value
(probably that's what you want), then you should to this:
for i in lista['abilities']:
dic_abi[i['ability']['name']] = i['ability']
or with dict comprehension:
dic_abi = {i['ability']['name']: i['ability'] for i in lista['abilities']}