Back to Freecodecamp

Step 6

curriculum/challenges/english/blocks/learn-lambda-functions-by-building-an-expense-tracker/666aae6a5d30a71f1fd7749f.md

latest1.3 KB
Original Source

--description--

The insert method can add an element at any position in a list. The first argument is the position at which the element has to be added, and the second argument is the element to add. For example, here's how to add a new element in the third position of example_list:

py
example_list = [4, 5, 6, 7]

example_list.insert(2, 5.5)

print(example_list) # [4, 5, 5.5, 6, 7]

Using insert(), add 1 to my_list in the proper position so that it is counting upward, then print the list.

--hints--

You should add 1 to my_list at index 1 using insert().

js
({
    test: () => runPython(`
    assert _Node(_code).has_call('my_list.insert(1, 1)')
    `)
})

my_list should have 4 elements.

js
({
    test: () => runPython(`
    assert len(my_list) == 4
    `)
})

my_list should equal [0, 1, 2, 3].

js
({
    test: () => runPython(`
    assert my_list == [0, 1, 2, 3]
    `)
})

You should print the list after adding the new element.

js
({
    test: () => runPython(`
    assert _Node(_code).is_ordered('my_list.insert(1, 1)', "print(my_list)")
    `)
})

--seed--

--seed-contents--

py
--fcc-editable-region--
my_list = [1, 2]

my_list.append(3)
print(my_list)

print(my_list[0])

my_list[0] = 0
print(my_list)

--fcc-editable-region--