0

I am a new programmer learning python with Codeacademy and I am supposed to create a function double_list that returns the items in list n and multiplies each number by 2. Every time I run the code I get the error: ' double_list([0, 1]) returned [0, 1] instead of [0, 2]'

n = [3, 5, 7]

def double_list(x):
  for i in range(0, len(x)):
    x[i] = x[i] * 2
    return x

print double_list(n)
1
  • 3
    Just pull out one indent before your "return". You currently return your value within the loop so that only the first item is multiplied. Commented Jul 17, 2015 at 15:22

6 Answers 6

4

Your function can only return once, so you need to back it out of the for loop. As written, your function currently will return x after the first iteration, so none of the remaining elements are modified.

def double_list(x):
    for i in range(0, len(x)):
        x[i] = x[i] * 2
    return x

An alternative, by the way, is to use a simple list comprehension, this will not modify the original list and will create a new one that you can assign back to the original variable if you'd like

def double_list(x):
    return [i*2 for i in x]

>>> n = [3, 5, 7]
>>> n = double_list(n)
>>> n
[6, 10, 14]

If you prefer to modify the actual list argument, you can use change the function to

def double_list(x):
    for index, value in enumerate(x):
        x[index] = 2 * value

>>> n = [3, 5, 7]
>>> double_list(n)
>>> n
[6, 10, 14]
Sign up to request clarification or add additional context in comments.

2 Comments

I also strongly recommend using a list comprehension rather than mutating the list in-place, however if you're going to change the list in-place, it's unnecessary to return AT ALL (much how list.sort() returns None -- it's just an in-place mutation)
@AdamSmith To make this more clear, I edited my post to show each method.
0

you can do it very easy with list comprehension.

n = [3, 5, 7]

def double_list(x):
  return [y*2 for y in x]

print double_list(n)
'[6, 10, 14]'

1 Comment

CoryKramer answered before me, you should accept his answer. good luck :)
0

This is an easy way to do it using map:

>>> n = [3, 5, 7]
>>> def double_list(x):
        return x * 2

>>> map(double_list, n)
[6, 10, 14]
>>> 

Comments

0

use map can simply it:

n = map(lambda x: 2*x, n)

Comments

-1
n = [3, 5, 7]

def double_list(x):
  for i in range(0, len(x)):
    x[i] = x[i] * 2
    return x

print double_list(n)

You just need to change n to x in the above line and you'll pass through this exercise

Comments

-1

You can simply do this:

n = [3, 5, 7]
def double_list(x):
    for i in range(0, len(x)):
        x[i] = x[i] * 2
        for j in range(1, len(x)):
            x[j] = x[j] * 2
            for k in range(2, len(x)):
                x[k] = x[k] * 2
        return x
# Don't forget to return your new list!

print double_list(n)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.