Remove a an 'inner' list inside of a list of dictionaries

Question:

I am working with pokeapi lately and im trying to filter some values out of it.
The JSON im working on is this one: https://pokeapi.co/api/v2/pokemon/golduck/.

What I need from this is to either find a way to remove the list ‘version_group_details’ that’s inside the big list [‘moves’] when the level_learned_at dictionary key has a value of 0 OR when the move_learn_method is NOT level-up. And then create a new list with this information, just with those specific keys and values.

My focus here is to just get a clean list of dictionaries with the moves that have a level-up value that’s higher than 0. (I’m trying to make a little table with only abilities learned by level-up).

Any assistance would be appreciated!

editing with the code I have now:

self.moves is just this: https://pokeapi.co/api/v2/pokemon/golduck/


        
        jsonfile = self.moves
        move = jsonfile['moves']
        lst = list(self.__evo_generator(move, 'move'))

        return lst

This would be what ‘gets’ me the abilities and the following is the generator im calling:

    def __evo_generatortwo(self):

        req = request.Request(
        self.moves,
        data=None,
        headers={
        'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.47 Safari/537.36'
        }
        )

        with request.urlopen(req) as r:
            data = loads(r.read())

        moves = [
            m['move']['name']
            for m in data['moves']
            if any(d['level_learned_at'] > 0 and d['move_learn_method']['name']=='level-up'
                for d in m['version_group_details'])
        ]
        print(moves)


UPDATED CODE

Up above I updated how the looks, although i am getting the following error: URLError
urllib.error.URLError: <urlopen error unknown url type: {'abilities'>

What could be the cause to this? Or did I make some mistakes when replacing the values with mine?

(I did go with the single list statement option since I don't feel confident enough to mess around with generators, haha)
Asked By: ehrgein

||

Answers:

If you just need any move that could be learned by leveling up, at any level (except 0) and without additional constraints:

from json import loads
from urllib import request

req = request.Request(
    'https://pokeapi.co/api/v2/pokemon/golduck/',
    data=None,
    headers={
        'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.47 Safari/537.36'
    }
)

with request.urlopen(req) as r:
    data = loads(r.read())


def __evo_generator(moves):
    for m in moves:
        if any(d['level_learned_at'] > 0 and d['move_learn_method']['name']=='level-up' for d in m['version_group_details']):
            yield m['move']['name']


for m in __evo_generator(data['moves']):
    print(m)

Note that I’ve removed the self, since you didn’t provide the rest of the code of the class, but you can of course turn the function into a method. And since the key is really a two-parter (‘move’ and ‘name’), I’ve left it out, but you could add that back in.

I wrote the function as a generator, since that was the route you were on, but you could also get the moves as a list in a single statement:

moves = [
    m['move']['name']
    for m in data['moves']
    if any(d['level_learned_at'] > 0 and d['move_learn_method']['name']=='level-up'
           for d in m['version_group_details'])
]
print(moves)

Edit: The first code example in the answer is intended to run all by itself, however in your original code, you probably only need the __evo_generator generator, or the second bit of code instead of it.

Answered By: Grismar
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.