-1

I'm not at ease with comprehension dictionaries I would like to transform this loop into a dictionary comprehension.

Thanks for your help

dico={}
for key in ['good','very good','bad','very bad','not good not bad']:
  if key in['good','very good']:
    dico[key]='green'
  else:
    dico[key]='red'
print(dico)

Here is what's expected

{'good': 'green',
 'very good': 'green',
 'bad': 'red',
 'very bad': 'red',
 'not good not bad': 'red'}
  • 4
    Does this answer your question? [How can I use if/else in a dictionary comprehension?](https://stackoverflow.com/questions/9442724/how-can-i-use-if-else-in-a-dictionary-comprehension) – Marcin Orlowski Nov 10 '22 at 10:21

2 Answers2

0

I think you you're looking to this

dico = {key: ['red', 'green'] [key in ['good','very good']] for key in ['good','very good','bad','very bad','not good not bad']}
  • I had the same idea, but the version from @AbdulNiyasPM in the comment to the question might be more explicit. – Matthias Nov 10 '22 at 10:27
0

Try this syntax

{k:'green' if k in ['good','very good'] else 'red' for k in ['good','very good','bad','very bad','not good not bad']}