# FAQ: List Comprehension - Squares

This community-built FAQ covers the “Squares” exercise from the lesson “List Comprehension”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Data Science

## FAQs on the exercise Squares

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply () below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

## Join the Discussion. Help a fellow learner on their journey.

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

Found a bug? Report it!

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

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

nums = range(11)
squares = [num * num for num in nums]
How does this work if “num” is not defined?
AND…
Why wont it accept my solution?
nums = range(11)
squares=[nums**2 for nums in nums]
print(squares)
I printed it and it works as asked but I get…
‘int’ object is not iterable

just like with a regular for loop:

``````for num in mums:
``````

the iterator (`num`) is defined in the loop

its really bad practice to give the iterator the same name as the iterable/list you are looping over.

I just tried the following code

``````nums = range(11)
num = range(10)
numnew = zip(nums,num)
print(numnew)
``````

from the previous examples, it should not print the list of numnew since i used only zip to create numnew but not list(zip(nums,num). But it worked correctly. Why did it happen?

1 Like

Hello, @erkandoan1782394440, and welcome to the forums.

That is a fluke in this particular exercise. The expected output would be something like this:

<zip object at 0x7fb4d4175648>

I went to the exercise, and replicated what you have shown. Just keep in mind that the behavior you’ve exhibited is definitely not what is expected in Python 3. I was able to mimic what you’ve shown using Python 2.7, so it would seem that in older versions of Python `zip()` created a list of tuples rather than a zip object. Let’s see:

``````#This is Python 2.7

nums = range(11)
num = range(10)
numnew = zip(nums,num)

print numnew
print type(numnew)
``````

Output:

[(0, 0), (1, 1), (2, 2), (3, 3), (4, 4), (5, 5), (6, 6), (7, 7), (8, 8), (9, 9)]
<type ‘list’>

Well, that appears to confirm the theory. Looks like this exercise is using the wrong version of Python.

Edit/Tip: To see which version of Python a particular exercise is using, you can add these lines to the top of your code:

``````import sys
print(sys.version)
``````

Output for this particular exercise:

2.7.15rc1 (default, Nov 12 2018, 14:31:15)
[GCC 7.3.0]

1 Like

Sadly, Python 2 has sang its swansong. We should by now be separated from that code in every respect. Even the language has cut ties with it. Python 3 is the currently supported and evolving version.

That said, expect Python 2 to have differences from what you encounter in web examples. The language presented with some minor issues that are resolved in version 3. However, that meant doing away with old regimes.

Going forward we may encounter old code. If it is still a needed component of the program then best port it to Python 3 so we know our whole program is supported. One supposes learning Python 2 will give us a leg up on recognizing old code. We would be wrong in assuming that. If all we know is Python 3, we’ll spot irregularities in a banker’s second. The first things I would look for are,

``````print a
``````

and

``````print range(a)
``````

and

``````raw_input()
``````

None of those behave the same way in Python 3.

1 Like

Why does this answer get an error? It would be more helpful to learn this from the content we pay for than search forums each exercise. I can do that for free.

nums = range(11)
squares = [nums**2 for num in nums]

We cannot square a range. Use the variable from your for loop.

1 Like