FAQ: List Comprehension - Double

This community-built FAQ covers the “Double” 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 Double

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 (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.

Ask or answer a question about this exercise by clicking reply (reply) below!

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

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

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!

How to multiplying each number in nums by two

my_list = [1, 2, 3]
index = 1

my_list[index] = my_list[index] * 2

print(my_list)

'''Output:
[1, 4, 3]
'''

Could you explain this please?

I don’t understand the index bit. (doesn’t help that the learn python 3 course doesn’t really have an index bit)

There is no index thing but as in the explanation:

nums = [4, 8, 15, 16, 23, 42]
double_nums = [num*2 for num in nums]

Hey!

I was just wondering… the item in the whole line code works as a variable exclusively to modify the list? And is it even a variable? Because I can name it anything and somehow Python understands that I am talking about the items in my list. But I don’t define it anywhere else in the code …
In that case, I guess I cannot use item for something else outside the [operation…] because it’s not going to be seen as defined, is that right?

Thanks a lot!

From that we can only ascertain that item is a variable declared in a for statement.

for item in a_list:

Or is this an incorrect presumption?

Okay so we declare a variable for that list only?

Yes, since that is the list we are iterating. The variable is there so actions in the code block have an identifiable object from that list.

Say we intend to iterate over every value in an iterable…

for temporary_value in iterable_object:

Both variables above are references, one to the iterable, and one to the loop instance. One reference is to the object, and the other is to an object IN the object.

1 Like

I liked the guy matt from codecademy from ohio , he explained it perfectly for me this is how he explains it , excellent imho , go matt

[operation for item in list]

so if you have a list already then follow this for more explantion for example

operation is where you would put number times to do multiply by 2
then replace the word “item” with number or whatever is stored in variable for example number or string replace the word “list” with the name of the variable i hope this makes sense

so for example

[operation for item in list] instead of this you would get

so “operation” would be replace by number * 2
so replace “item” would be number or string or whatever is stored in the variable which is number in this case
replace list with the variable name before the = sign

so for example this

operation for item in list

double_nums = [number * 2 for number in nums]

print(double_nums)

hope this makes sense if not then please let me know thanks Ian