-1

I want to create an empty dict from A to Z without J and Y.

my_dict = {x: [0] * 15 for x in string.ascii_uppercase}

With this, I create an empty dict with all alphabets but I want a list without J and Y. How I can make this.

Thanks a lot for any help :)

B.T
  • 57
  • 5

5 Answers5

4

You can have conditions in dict comprehension

my_dict = {x: [0] * 15 for x in string.ascii_uppercase if x not in "YJ"}
Eternal
  • 928
  • 9
  • 22
2
my_dict = {x: [0] * 15 for x in string.ascii_uppercase if x not in ['J','Y']}
Dishin H Goyani
  • 7,195
  • 3
  • 26
  • 37
gsb22
  • 2,112
  • 2
  • 10
  • 25
1

Just use a hard-coded string, you could use an if statement but there isn't any need to

my_dict = {x: [0] * 15 for x in "ABCDEFGHIKLMNOPQRSTUVWXZ"}
Sayse
  • 42,633
  • 14
  • 77
  • 146
1
{x: [0] * 15 for x in string.ascii_uppercase if x not in 'JY'}
RikkiH
  • 107
  • 5
1

my_dict = {x: [0] * 15 for x in string.ascii_uppercase if x not in ["J", "Y"]}

This should do the job

Arkenys
  • 343
  • 1
  • 11
  • Why create a list ["J", "Y"]? @Arkenys – Eternal Feb 12 '20 at 09:31
  • Because the solution with a list is working maybe ? What is the purpose of providing the exact same solution as others that already answered with ```in "JY"``` ? – Arkenys Feb 12 '20 at 09:33
  • That solution requires a list creation which is not as optimized as "JY". Also not readable (comparing to simple string). – Eternal Feb 12 '20 at 09:34
  • If you think a list is not readable, then I won't imagine how more complicated structures appear to you tbh. It's maybe less optimized because required a list creation, but it also show that you can use a list to do the same job. Which, in other cases than just working with simple chars could be useful. – Arkenys Feb 12 '20 at 09:37