I need help understanding this code, it's a for loop inside a function and has to do with a dictionary

Hi, I’m new here and I’m doing my best to learn Python. Your help is much appreciated.
I’m building a slot machine. The values in the dict. are the slot values." A" : 2 means there are two A’s “B” : 4 there are 4 B’s etc…

I don’t understand this line:

for symbol, symbol_count in symbols.items():

symbol has never been a variable, is it from the variable symbol_count? If so, how does the code know what symbol is if it has not been a variable or defined?

The rows, cols, symbols in def get_slot_machine_spin(rows, cols, symbols): is also slightly confusing although I somewhat understand it.

symbol_count = {
  "A": 2,
  "B": 4,
  "C": 6,
  "D": 8
}

def get_slot_machine_spin(rows, cols, symbols):
  all_symbols = []
  for symbol, symbol_count in symbols.items():
    for _ in range

You don’t need to define variable names in Python; you just assign to a name like you did when you wrote:

symbol_count = {
...
}

or when you wrote names in the parameter list of get_slot_machine_spin or when you wrote names after the for.

I don’t understand this line:

for symbol, symbol_count in symbols.items():

Yes it is. For loops create one or more variables:

for x in ...  # creates one variable x

for symbol, symbol_count in ...  # creates two variables, symbol and symbol_count

In your case, you are interating over the items belonging to the dict symbols. The dict has some (key: value) pairs, and so the for-loop will give you:

# first loop
symbol = "A"; symbol_count = 2
# second loop
symbol = "B"; symbol_count = 4
# third loop
symbol = "C"; symbol_count = 6
# fourth loop
symbol = "D"; symbol_count = 8

You can see this for yourself by inserting a call to the print function inside the loop:

for symbol, symbol_count in symbols.items():
    print("symbol", symbol, "symbol_count", symbol_count)
    ...
2 Likes

Thank you! I understand now. I can’t tell you how much I appreciate your help. - Jason

I’m not sure it this will make it more or less clear, but there is a lot going on here in that one line of code :slight_smile:

For loops:

for x in something:

iterates through “something” (which must be an “iterable”[*] – often a sequence of some sort). each time through the loop, x gets assigned to the next item in the iterable. A very simple example:

In [1]: for x in (4,1,6,3):
   ...:     print(x)
   ...: 
4
1
6
3

dict.items() :

A dictionary’s items() method returns an iterable of the “items” in the dictionary – each iteration will return a tuple of one item: (key, value):

In [5]: symbol_count = {
   ...:   "A": 2,
   ...:   "B": 4,
   ...:   "C": 6,
   ...:   "D": 8
   ...: }

In [6]: for an_item in symbol_count.items():
   ...:     print(an_item)
   ...: 
('A', 2)
('B', 4)
('C', 6)
('D', 8)

Sequence unpacking:

If you assign more than one name, Python will “unpack” the seqence on the right, and assign them to the names on the left:

In [7]: a, b = (5, 6)

In [8]: a
Out[8]: 5

In [9]: b
Out[9]: 6

Note that you don’t need the () on the right – the comma will also make a tuple that can be unpacked.

In [10]: a, b = 5, 6

In [11]: a
Out[11]: 5

In [12]: b
Out[12]: 6

Putting this all together, you can also “unpack” in a for statement, so that the (key, value) tuple being returned by dict.items() gets auto assigned to the two names provided:

In [16]: for key, value in symbol_count.items():
    ...:     print(f"{key=},{value=}")
    ...: 
    ...: 
    ...: 
key='A',value=2
key='B',value=4
key='C',value=6
key='D',value=8

I couldn’t help myself but introduce the nifty f-string debug options, too :slight_smile:

so this line:

 for key, value in symbol_count.items():

Is actually doing all of this:

items = symbol_count.items()
for item in items:
    key = item[0]
    value = item[1]

Clear as mud?

[*] There’s more to learn about iterables, but in short: they are something you can put in a for loop that will returns items until done.

4 Likes

Hi Christopher, what a great explanation! Thank you for helping. It’s a lot to take in but I think I understand it now. It’s definitely more clear! -Jason

1 Like