Pedro Abreu
Pedro Abreu

Reputation: 99

Replacing string with tuple in list of tuples

I am very sorry to be asking this kind of newbie question but here it goes. So I basically have this returned as levels:

levels = [(1, 210, 30, 500, 500, 'white'),(1, 210, 30, 200, 400, 'white'),(1, 210, 30, 600, 300, 'white')]

And I want to iterate through it and replace 'white' with white which is simply (255,255,255). Python complained about tuples not being changeable, so I would need to create a new list with the tuples instead of white. Is there a quick way to do this?

Upvotes: 0

Views: 139

Answers (2)

L3viathan
L3viathan

Reputation: 27273

If you want to keep it as a list of tuples, you can try:

replacements = {"white": (255, 255, 255)}
levelsList = [(a, b, c, d, e, replacements.get(f, f)) for a, b, c, d, e, f in levels]

Upvotes: 1

Stefan Reinhardt
Stefan Reinhardt

Reputation: 622

just do

levelsList = [list(x) for x in level]

After that you can change the 'white' string

Upvotes: 1

Related Questions