I have tried googling this but the google results just tell me how to convert a list into a dictionary, which is not what I want to do.
I have a list: colors = ['Red', 'White', 'Green', 'Red']
and I have a dictionary: color_dict = {'Red': 0, 'White': 1, 'Green': 2}
I want to end up with converted_colors = [0, 1, 2, 0]
I have tried converted_colors = [color for colors in color_dict[color]]
and converted_colors = [color for color_dict[color] in colors]
but both give the same error NameError: name color is not defined
Is my approach all wrong? Any help would be much appreciated.
CodePudding user response:
Here an example
converted_colors = [color_dict[color] for color in colors]
Here the output
[0, 1, 2, 0]