Search code examples
pythonpokeapi

How can i store for loop print value in variable or list


I am actually new to python and just started learning it i am working with an module which is based on Pokeapi and its name is Pokebase which is basically a python wrapper for that api so I am having a problem with it: Here's a for loop in which I print some data recieved by the api. Code:

import pokebase as pb
p1=pb.pokemon('charmander')
for stat in p1.stats:
    print('{}: {}'.format(stat.stat.name, stat.base_stat))

actually I dont want to print this value I want to save this value in a variable or list how can i do that?


Solution

  • If you want to save it in a list:

    [[stat.stat.name, stat.base_stat] for stat in p1.stats]
    

    This is called a list comprehension.

    If you want to use a dictionary:

    {stat.stat.name: stat.base_stat for stat in p1.stats}