Python for x on main question list

I am trying to create a function that will load many images and map them to the corresponding names in PyGame. I'm not so good with python and it really made me stuck. My current code is:

tile1 = pygame.image.load("/one.bmp")
tile2 = pygame.image.load("/two.bmp")
tile3 = pygame.image.load("/three.bmp")

and he continues to work about 20 tiles. The thing is, I only found out that I needed a lot more, and I was wondering how I can do this using for for x in y loop. My main idea:

tile = ['/one.bmp', '/two.bmp', '/three.bmp']
tilelist = [1,2,3]
for tile in tile:
    tilelist[x] = pygame.image.load(tile)

or something like that, but I'm not quite there. I was also wondering if this can be done using dictionaries.

Any help would be appreciated, thanks :)

+3
source share
2 answers

List of concepts for salvation.

tiles = ['/one.bmp', '/two.bmp', '/three.bmp']
tilelist = [pygame.img.load(tile) for tile in tiles]

As @isakkarlsson commented,

... or easier (?) tilelist = map(pygame.img.load, tiles)

+9

tile = ['/one.bmp', '/two.bmp', '/three.bmp']
imageMap = {}
for t in tile:
    imageMap[t] = pygame.img.load(t)

, imageMap.keys() , .

+1

All Articles