FAQ: Learn Python: Python Lists and Dictionaries - More with 'for'

Community%20FAQs%20on%20Codecademy%20Python%20Exercises

This community-built FAQ covers the “More with ‘for’” exercise in Codecademy’s lessons on Python.

FAQs for the Codecademy Python exercise eMore with ‘for’:

Join the Discussion. We Want to Hear From You!

Have a new question or can answer someone else’s? Reply (reply) to an existing thread!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources about Python in general? Go here!

Want to take the conversation in a totally different direction? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account, billing, Pro, or Pro Intensive? Reach out to our support team!

None of the above? Find out where to ask other questions here!

Other FAQs

The following are links to additional questions that our community has asked about this exercise:

  • This list will contain other frequently asked questions that aren’t quite as popular as the ones above.
  • Currently there have not been enough questions asked and answered about this exercise to populate this FAQ section.
  • This FAQ is built and maintained by you, the Codecademy community – help yourself and other learners like you by contributing!

Not seeing your question? It may still have been asked before – try (search) in the top-right of this page. Still can’t find it? Ask it below by hitting the reply button below this post (reply).

2 posts were split to a new topic: How does Python decide the sort order by default

A post was merged into an existing topic: How does Python decide the default sort order?

Hello,
I’m trying to understand if the code I have below will become an issue in a real use scenario as opposed to what the correct solution was – I did not define a new variable in the for loop, i.e. “for number in start_list:”, the output was the same so I am not sure if this is very incorrect by me.

Thanks

for start_list in start_list:

square_list.append(start_list ** 2)
square_list.sort()

print square_list

It’s way too soon to be worrying about that. Nothing we do at this stage is going to overburden the system. Concentrate on learning concepts and syntax, and not on the better way to do things. Stick to the lessons and don’t stray much. It’s counterproductive and disruptive.

2 Likes

roger that :slight_smile: thanks!!

1 Like

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

Your code here!

for number in start_list:
square_list.append(number ** 2)
square_list.sort()

print square_list

How does python know that the variables inn the list correspond to the variable number used in the for statement?

3 Likes

Because we defined it in the for statement as the variable to use with the in operator.

in cycles through the list object, one item at a time, which it assigns temporarily to the number variable that we supplied for this purpose.

There are five items in the list above, so number will be in turn,

number = 5
number = 3
number = 1
number = 2
number = 4

When the operator reaches the end of the list, the loop terminates.

4 Likes

What is the purpose of square_list = ? Does it define square_list as the numbers in start_list’s brackets? I was wondering how we were able to declare square_list.append a few lines down without any clear declaration of what it was.

First: define a new, empty list, call it square_list
Next: go through the existing list, start_lst
— and, for each element in start_list:
-------- obtain the value of that element, squared
-------- and append that value to square_list
When you are finished, you will have two lists, the original start_list, which is unchanged, and square_list, the list of squares.

In the above logic, the reason for the statement, square_list = [] is to provide a place to store the squared values.

1 Like

What changes when i swap the place of number and ** 2. For example here is my code
start_list = [5, 3, 1, 2, 4]
square_list =

Your code here!

for number in start_list:
square_list.append(2 ** number)
square_list.sort()

print square_list

which gives me a result of [ 2, 4, 8, 16, 32 ]

Can you tell us what changes? What is the difference between 2**5 and 5**2 ? (Hint: if you are playing chess for grains of rice, choose the former!)

2 Likes

Once again, a great answer. I’m not sure how I glossed by this but it’s so simple when you actually think about the numbers. Perhaps I need to drink some coffee in the morning before asking these questions. So I see why i should write it the original way.

Thanks again for such a good answer for my silly brain.

1 Like

my code:

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

Your code here!

for number in start_list:
number = number ** 2
square_list.append(number)
square_list.sort()

print square_list

printed [1, 4, 9, 16, 25] to console as expected.

but if I remove square_list = on line 2, it prints:
[1, 1, 4, 4, 9, 9, 16, 16, 25, 25].

my question is why it prints double numbers just because we didn’t declare square_list as an empty list, and not resulted in error…

Even though you removed the statement, because you ran the code with it before you removed it, the variable is still in memory. Refresh the page and try again. It should raise an exception.

I’m in the segment python lists and dictionaries, and i am confused about what to do for the excercise more with ‘for’. I just really dont understand at all what i am supposed to do can someone help me undertand this please becuase i am completely lost and need some guidence on how to do this.

Please post a link to the question, and any code you have attempted, thus far. We need to be clear about what you do and do not understand.

Was for not brought up before now? Did you learn how to count with a for loop, the in keyword, and a range()?

I retyped exactly the same code several times, but why each time the console is giving me square_list not defined?

The error you got suggests that square_list was not properly defined. Did you assign square_list to an empty list? And is square_list globally accessible? Posting your code here will help to determine what the exact problem may be.

Welcome to the forums!

1 Like