# More for 'for' - going out of my mind

#1

I just cannot figure out what I’m supposed to do. I have some fragments of the correct answer, but I don’t know where they go or what I’m supposed to do with them.

start_list = [5, 3, 1, 2, 4]
square_list = []

# Your code here!

print square_list

I know I need a “for variable in list” type statement, but I cannot figure out what comes after “for” and what comes after “in”. The obvious first guess is “for square_list in start_list” but I don’t see why or how that could be correct. The second guess, and (the way I see it) only other possible option is “for start_list in square_list:” but that seems even more wrong. Either configuration just looks like self-referential nonsense to me. It seems like it has to be one of those two ways, but both seem completely wrong.

I think my code needs to contain

square_list.append(start_list ** 2)

and

square_list.sort()

I don’t want to get through these lessons by brute forcing as many possibilities as I can think of just to get the green box with a checkmark–I want to understand why what I’m doing is correct. Nevertheless, I did try it the way I have it here, and it was wrong.

start_list = [5, 3, 1, 2, 4]
square_list = []

for square_list in start_list:
[indent] square_list.append(start_list ** 2)
[indent] square_list.sort()

print square_list

#2

You won’t want to re-use the variable intended to contain your result.

``````for n in start_list:
``````

Then you will append some outcome using `n` as the operand.

#3

So I’m supposed to create my own new variable for the for/in statement?

#4

Yes. It can be any variable, just not one that already exists.

#5

start_list = [5, 3, 1, 2, 4]
square_list = []

for n in start_list:
[indent]square_list.append(n ** 2)
[indent]square_list.sort()

print square_list

Thank you. This turned out to work, but I’m struggling to see how it worked. I’m having a lot of trouble wrapping my head around the for/in format.

for n in start_list

The way I understand it, this statement is supposed to apply the new variable n, in some way, to every item in start_list. Then we line-break, indent, and start making up rules… but do the rules just apply to n? Or can they also apply to start_list?

I’m having a really hard time wrapping my head around this. I don’t even know what questions to ask, I just feel stumped. The worst part is it seems so simple and I feel like I should be able to grasp it on the first try.

#6

`for..in` iterates the object specified by `in`, one element at a time, hence the local variable, `n` (or any other name).

``````for _value_ in _list_:
``````

Technincally speaking, `in` is a type of `iterator` that keeps track of which value is `next`. Recall that iterators have a `next` attribute.

We don’t wish for you become bogged down in this topic so keep up your lessons and be sure to do a full review at the end of each unit. This topic will emerge in time. Keep asking questions when you are stuck.

#7

not an operator, that could not possibly work, the `in` operator (which this is not) does nothing to help iteration

#8

I stand corrected. Thanks.

does nothing to help iteration

So is it the object that has a `next` attribute or is it an `__iter__`.? Sorry, need to do so refresher reading.

#9

Thank you. You’ve been very helpful with this stuff (and this hasn’t been the first time you’ve helped me). I really appreciate it.

#10

Meh. Let me at it.

Values define their own behaviour.
One such behaviour is iteration.
A for-statement invokes that behaviour, repeatedly asking for next value until there is no more

(most operators are such behaviour, addition multiplication etc, and also lookup by key with `[]` (which is an operator) and attribute lookup (period , `.`)

#11

@mtf
iterable values implement `__iter__` which returns an iterator. An iterator implements `__iter__` because it is iterable (and returns itself), and also `__next__` which corresponds to the builtin function `next`

``````class Cat:
def __iter__(self):
return ('Meow' for _ in range(10))

print(list(Cat()))
``````
``````['Meow', 'Meow', 'Meow', 'Meow', 'Meow', 'Meow', 'Meow', 'Meow', 'Meow', 'Meow']
``````

The generator expression there creates an iterator (thus sparing Cat from defining `__next__` and `__iter__`

@thegovernment0usa ignore the details here. It’s far more features than you’ve gotten around to looking at. The part that matters here is that values define their own behaviour. How exactly it’s done is a topic for far later, if it’s ever interesting or useful.

#12

Very useful explanation, thanks for putting me straight.

Just found a decent page on the subject…

https://www.programiz.com/python-programming/iterator

#13

Ah okay. I was hoping this wasn’t supposed to be stuff I already knew. I’m exploring the Python term “iterate” on a wiki.

#14

You might note though, that I defined a class (class is 95% synonymous with type in python) which defines how its values are iterated over.
list (which is also a class) was able to use that behaviour to create a list by iterating over the Cat instance that I made.
Having special names for particular behaviour is how python objects (95% synonymous with value in python) usually communicate with each other (such as creating a list from a Cat)

#15

Oh and this is what object oriented means.
“object oriented” includes a whole lot of things, much of which python doesn’t care about. In python it’s mostly just that values have their own behaviour as opposed to being stupid data that actions have to be done to

#16

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.