-4

For a school assignment, I have to adapt a given code but keep the dictionary comprehension mechanism in Python. Given my code

dct = {k: v for k in ["HELLO", "SLEEPING"] for v in ["WORLD", "CITY"]}
print(dct["HELLO"])

the printout is "CITY". How do I have to adapt the code to get "WORLD" returned? Thank's a lot!

1 Answers1

1

Use zip

dct = {k: v for k, v in zip(["HELLO", "SLEEPING"], ["WORLD", "CITY"])}
print(dct["HELLO"])

And if you can get rid of the dictionary comprehension:

dct = dict(zip(["HELLO", "SLEEPING"], ["WORLD", "CITY"]))
rdas
  • 20,604
  • 6
  • 33
  • 46