what happens when you substract a scalar from a list of values in python

Subtracting a scalar from a list in Python using the mathematical operator - resulting in a new list. The scalar value is subtracted from each element of the list individually. Here is an example:

main.py
my_list = [10, 20, 30, 40, 50]
scalar = 5
new_list = [x - scalar for x in my_list]
print(new_list)
99 chars
5 lines

Output:

main.py
[5, 15, 25, 35, 45]
20 chars
2 lines

In the above example, we subtracted 5 from each element of the list my_list and stored the result in new_list.

related categories

gistlibby LogSnag