0
How does this Python code execute to 17?
Hello! Iām learning Python and Iām trying to understand why things work before continuing and Iāve just been stumped with this very simple code: primes = {1: 2, 2: 3, 4: 7, 7: 17} print(primes[primes[4]]) Which comes out to 17. I was wondering how it gets 17 from this list. At first I thought it was because it takes the fourth dictionary item, but upon change the 4 to a 3, it gives me a KeyError (which as you know means the item is not in the list). If anyone can explain how this code gets 17, reply.
1 Answer
+ 5
Solve from inner to outer:
primes[4] gives 7
so we can write it like
primes[7]
primes[7] gives 17
The data structure is a dictionary, not a list.