-2
dict1={"a":4,"b":2,"A":6}
print({k.lower():dict1.get(k.lower(),0) + dict1.get(k.upper(),0) for k in dict1.keys()})

I copied this code from a youtube video. I couldn't understand the code properly. Please help me to figure it out. I couldn't understand the purpose of 0 in second line.

k.lower():dict1.get(k.lower(),0)

I'm a beginner in python. kindly help me

rioV8
  • 24,506
  • 3
  • 32
  • 49
Muaviya
  • 1
  • 3

1 Answers1

0

The second parameter is the value parameter, that is , if the key does not exist this will be the value.
In your example, dict1["B"] does not exist. Normally that would cause an error. But because the value parameter is 0, instead of causing an error it pretends that dict1["B"] is 0. Note that this does not change the original dictionary.\

Vas
  • 130
  • 10