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.

14th Oct 2022, 1:15 PM
Noah Hancock
1 Antwort
+ 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.
14th Oct 2022, 1:28 PM
Lisa
Lisa - avatar