-4

Given this list

l = ['A','B','C','A']

how can I map it to a list of unique colors, e.g. ['red','blue','green'] without creating manually a dictionary?

The result would be

lc = ['red','blue','green','red']
I'mahdi
  • 23,382
  • 5
  • 22
  • 30
user2287387
  • 39
  • 11
  • You can create a dictionary : ```d1={'A':'red', 'B':'blue', 'C:'green''}``` and then ```[d1.get(x) for x in lc]``` –  Aug 03 '21 at 13:12
  • Sorry I forgot to specify that I don't want to create manually a dictionary – user2287387 Aug 03 '21 at 13:14
  • 1
    Then how exactly do you want to associate the values? Is first come assigned fine? Or random? – tituszban Aug 03 '21 at 13:15
  • It doesn't matter as long as the same values in the list are mapped to the same color – user2287387 Aug 03 '21 at 13:17
  • 1
    That means you need somehow to save which letter goes to which color. In other words a mapping. In other words a dictionary. If you don't want to manually assign them, you can do it dynamically but you will still need a dict. – Tomerikoo Aug 03 '21 at 13:42

1 Answers1

1

If you want to map some value, you most likely want to use a dictionary:

colours = {"A": "red", "B": "blue", "C": "green"}

lc = [colours[v] for v in l]

As a result of which lc has this value:

['red', 'blue', 'green', 'red']
tituszban
  • 4,797
  • 2
  • 19
  • 30