3

I have a list as such:

pokemonList = ['Ivysaur', 'Grass', 'Poison', '', 'Venusaur', 'Grass', 'Poison', '', 'Charmander', 'Fire', ''...]

Note that the pattern is 'Pokemon name', 'its type', ''...next pokemon

Pokemon come in both single and dual type forms. How can I code this so that every pokemon (key) will have their respective type(s) applied as its value?

What I've got so far:

types = ("", "Grass", "Poison", "Fire", "Flying", "Water", "Bug","Dark","Fighting", "Normal","Ground","Ghost","Steel","Electric","Psychic","Ice","Dragon","Fairy")
pokeDict = {}
    for pokemon in pokemonList:
        if pokemon not in types:
            #the item is a pokemon, append it as a key
        else:
            for types in pokemonList:
                #add the type(s) as a value to the pokemon

The proper dictionary will look like this:

{Ivysaur: ['Grass', 'Poison'], Venusaur['Grass','Poison'], Charmander:['Fire']}
avereux
  • 572
  • 1
  • 5
  • 15

4 Answers4

3

Just iterate the list and construct the item for the dict appropriately..

current_poke = None
for item in pokemonList:
    if not current_poke:
        current_poke = (item, [])
    elif item:
        current_poke[1].append(item)
    else:
        name, types = current_poke
        pokeDict[name] = types
        current_poke = None
Chad S.
  • 6,252
  • 15
  • 25
2

Recursive function to slice up the original list, and a dictionary comprehension to create the dict:

# Slice up into pokemon, subsequent types
def pokeSlice(pl):
    for i,p in enumerate(pl):
        if not p:
            return [pl[:i]] + pokeSlice(pl[i+1:])      
    return []

# Returns: [['Ivysaur', 'Grass', 'Poison'], ['Venusaur', 'Grass', 'Poison'], ['Charmander', 'Fire']]

# Build the dictionary of 
pokeDict = {x[0]: x[1:] for x in pokeSlice(pokemonList)}

# Returning: {'Charmander': ['Fire'], 'Ivysaur': ['Grass', 'Poison'], 'Venusaur': ['Grass', 'Poison']}
leroyJr
  • 1,110
  • 9
  • 17
1

Here's the low-tech way to do it: Iterate over the list and collect records as you go.

key = ""
values = []
for elt in pokemonList:
    if not key:
        key = elt
    elif elt:
        values.append(elt)
    else:
        pokeDict[key] = values
        key = ""
        values = []
alexis
  • 48,685
  • 16
  • 101
  • 161
  • This code is really neat and breezy to read. I wish I could accept both answers! – avereux Nov 03 '15 at 23:04
  • No problem, it's your prerogative to accept whichever answer you like better. (But you can upvote as many answers as you wish ;-) – alexis Nov 03 '15 at 23:09
1

One liner. Not because it is useful but because I started trying and had to finish.

>>> pokemon = ['Ivysaur', 'Grass', 'Poison', '', 'Venusaur', 'Grass', 'Poison', '', 'Charmander', 'Fire', '']
>>> { pokemon[i] : pokemon[i+1:j] for i,j in zip([0]+[k+1 for k in [ brk for brk in range(len(x)) if x[brk] == '' ]],[ brk for brk in range(len(x)) if x[brk] == '' ]) }
{'Venusaur': ['Grass', 'Poison'], 'Charmander': ['Fire'], 'Ivysaur': ['Grass', 'Poison']}
RobertB
  • 1,879
  • 10
  • 17