Computer Science
Write a program to increment the elements of a list with a number.
Python
Python List Manipulation
81 Likes
Answer
lst = eval(input("Enter a list: "))
print("Existing list is:", lst)
n = int(input("Enter a number: "))
for i in range(len(lst)):
lst[i] += n
print("List after increment:", lst)
Output
Enter a list: [1, 2, 3, 4, 5]
Existing list is: [1, 2, 3, 4, 5]
Enter a number: 10
List after increment: [11, 12, 13, 14, 15]
Answered By
36 Likes
Related Questions
What is the output of the following code?
numbers = list(range(0, 51, 4)) results = [] for number in numbers: if not number % 3: results.append(number) print(results)
Following code prints the given list in ascending order. Modify the code so that the elements are printed in the reverse order of the result produced by the given code.
numbers = list(range(0, 51, 4)) i = 0 while i < len(numbers): print(numbers[i] , end = " ") i += 3 # gives output as : 0 12 24 36 48
Write a program that reverses a list of integers (in place).
Write a program that inputs two lists and creates a third, that contains all elements of the first followed by all elements of the second.