Have a go at it. The editor and the docs panel are open, and your code is saved as you type. Running it needs a free account — you’ll come back to exactly what you wrote.

Building a List

~8 mincode completion

Lists can be changed after they are made. The pattern you will use most often is: start empty, add as you go.

result = []
result.append("a")
result.append("b")
result            # ["a", "b"]

Other things you can do:

nums = [3, 1, 2]
nums.append(4)        # [3, 1, 2, 4]
nums.sort()           # [1, 2, 3, 4]   sorts in place, returns None
sorted(nums)          # returns a NEW sorted list, leaves nums alone
len(nums)             # 4
nums + [5, 6]         # [1, 2, 3, 4, 5, 6]  a new list

Watch out for one specific trap: nums.sort() returns None. Writing nums = nums.sort() throws your list away and leaves you with None. Either call nums.sort() on its own line, or use sorted(nums).

Your task:

Write double_all(values) that returns a new list where every number has been doubled. Leave the input list unchanged.

For [1, 2, 3] it returns [2, 4, 6].

Example Tests

Each number comes back doubled

Input: {"values":[1,2,3]}

Expected: [2,4,6]

Zero and negatives double correctly

Input: {"values":[0,-4,5]}

Expected: [0,-8,10]

An empty list gives an empty list

Input: {"values":[]}

Expected: []

Python
def double_all(values):
    """
    Double every number in a list.

    Args:
        values: a list of numbers

    Returns:
        A new list with each number doubled. The input is not modified.
    """
    # YOUR CODE HERE
    pass

Run your code to see results

⌘↵ runs against the visible tests

Loading docs…