Significance of not searching for element and launching straight into for

On previous examples for looks the syntax has been element for element in old list [do something]

on this exercise however,


The hint suggests we launch straight into the for statement so I believe I have misunderstood the syntax and how we form these comprehension statements. Would someone be able to advise please. Is it because in this case we are not transforming the element like we did previously by performing maths for Fahrenheit or adding 100 for upvotes?

single_digits = list(range(10))
for writeout in single_digits:

Apologies if I’ve misunderstood your query but you can use for loops on any iterable or expression that can return one in Python. Iterables cover all sorts of things which can return their contents one element at a time which includes standard sequences, dictionaries and range amongst many others.
Your code above could be rewritten with-

for writeout in range(10):

I’m not sure what issue you have with transforming the element. Within the loop your variable, writeout in this case acts like an object of the same type would anywhere else until the next iteration of the loop occurs and that name is overwritten by the next element of your iterable. If necessary could you explain your issue further, ideally with an example?


I don’t understand why the solution is not

single_digits = list(10))
single_digits for writeout in single_digits:

That looks like a possible syntax error. What is the purpose of the single_digits name before the for loop? Double check those brackets too.

Note that even without this addition your code is not equivalent because all list(10) does is create a list around a single element, an integer 10. It’s the same as [10] which is not equivalent to range(10).

In your original post you mentioned comprehension? That is a different style to the standard for loop and would like the following where the [ ] brackets are essential-

mylist = [x for x in range(3)]
[0, 1, 2]

Hi @tgrtim lol that’s what my query is when would you need to reference the list before a for statement and when would you just say
"for x in x…

Apologies for the extra bracket in the previous message that was indeed a typo

Unless it’s a list comprehension which has its own syntax then you wouldn’t need to save the list or any other iterable to a variable beforehand. You could just use it within the expression section of the for loop, as a blunt example-

for x in list((1, 2, 3)):

There’s nothing wrong with saving it beforehand for the sake of readability of if you’re going to use it again within the script.

Gotcha, I decided to revisit the lessons again (now I can give them my undivided attention without a noisy background) and it makes much more sense and thank you so much for your help. Your “you wouldn’t need to save…to a variable” made me realise that’s all it was doing!
Ta very much :smiley: I think its one of those things where I will look back on this one day and cringe at what I was asking but atm when you start ploughing through the lessons every punctuation mark just stacks up and becomes overwhelming! Anyway thank you again

1 Like