Desura
Desura

Reputation: 175

Key error '0' with dict format

I'm still a beginner in Python, and I wanted to know why this :

    dict = {}
    dict[0] = '123'
    a = 0
    if dict["{}".format(a)]["{}".format(a)] == '1':
        print('True')

gives me a Key Error '0' but not this :

    dict = {}
    dict[0] = '123'
    if dict[0][0] == '1':
       print('True')

Thanks in advance.

Upvotes: 2

Views: 18258

Answers (1)

Reloader
Reloader

Reputation: 718

You're trying to compare the key 0 with "0". They are different. One is an integer and another is a string.

Upvotes: 10

Related Questions