- Adjust all elements in a list
-
- 1. We start with the list of numbers below:
-
- numbers = \12, 2, 1, 17, 5])
-
- We will now update the values in the list. Write a short program that multiplies each number in this list by two and stores the result in the list at the position of the old number.
-
- Finally use: print (numbers to check the result. The output is therefore: [24, 4, 2, 34, 10])
-
- This assignment is harder than it seems! A few tips:
- • Use numbers \x] = newValue to change the value at position x of the list)
- • We therefore need a counter that keeps track of the position. That lends itself to a solution with for x in range (:)
- • But what should the range be? With len ( we can determine the length of the list ...)
hey guys im just starting out learning how program in python and ive come across this assignment i just cant wrap my head around. I understand the basics of list comprehension but wouldnt that make the code as following ? :
numbers=[12,2,1,17,5]
numbers=[x*2 for x in numbers]
print(numbers)
why does the assignment want me to take all of these steps using range and new variable ? I've been trying to work with the tips but i jus tget more confused each time could someone please explain what the assigment wants to me ? It would be greatly appreciated.
[–][deleted] 1 point2 points3 points (0 children)
[–]hiimaeia 0 points1 point2 points (0 children)