You can declare a list
new_list = []
and inside the loop, use append()
method to add the element:
for i in n:
x = i + 5
new_list.append(x)
You can also do this by list comprenhension:
new_list = [i + 5 for i in n]
You can declare a list
new_list = []
and inside the loop, use append()
method to add the element:
for i in n:
x = i + 5
new_list.append(x)
You can also do this by list comprenhension:
new_list = [i + 5 for i in n]
Using list comprehension is quite simple:
n=[1,2,3,4,5,6,7,8]
result = [i+5 for i in n]
print(result)