What is enumerate?

Started by
2 comments, last by Bacterius 8 years, 11 months ago

Hi,i just read a code and there is an enumerate function but i don't know what enumerate do?I read some question and it appear that enumerate is giving index for each loops but if i delete the enumerate it give me an error so i think there are some things that i missed.

Here the error:


 too many values to unpack(expected 2) 

And here here the code:


def load_tile_table(filename, width, height):
	image = pygame.image.load(filename).convert()
	image_width, image_height = image.get_size()
	tile_table = []
	for tile_x in range(0, image_width//width):
		line = []
		tile_table.append(line)
		for tile_y in range(0, image_height//height):
			rect = (tile_x*width, tile_y*height, width, height)
			line.append(image.subsurface(rect))
	return tile_table

if __name__ == '__main__':
	pygame.init()
	screen = pygame.display.set_mode((128,98))
	screen.fill((255, 255, 255))
	table = load_tile_table('ground.png', 24, 16)
	for x, row in enumerate(table):
		for y,tile in enumerate(row):
			screen.blit(tile, (x*32, y*24))
	pygame.display.flip()
	while pygame.event.wait().type != pygame.locals.QUIT:
		pass

Thank you very much smile.png

Advertisement
You need to learn how to look for information on the web yourself. I know very little about Python, but it took me about 10 seconds to find the answer to your question: https://docs.python.org/2/library/functions.html#enumerate

You need to learn how to look for information on the web yourself. I know very little about Python, but it took me about 10 seconds to find the answer to your question: https://docs.python.org/2/library/functions.html#enumerate

Ok thank you smile.png

Yeah, the Python docs are really good. Make sure you're looking at the right docs for your version though! (2 vs 3)

“If I understand the standard right it is legal and safe to do this but the resulting value could be anything.”

This topic is closed to new replies.

Advertisement