Subtract a Value from Every Number in a List in Python

Subtract a value from every number in a list in Python?

With a list comprehension:

a = [x - 13 for x in a]

Is there a way to subtract a number from every number in a list above another number in python?

You can try this.

a = [1, 2, 3, 5, 6, 7, 8, 9]
[i-1 if i>4 else i for i in a]
# [1, 2, 3, 4, 5, 6, 7, 8]

Subtract values in a list from the first value (Python)

Try this

myList = [3,2,1,4,5]

def subtractionDifference(n):
difference = n[0]

for num in n[1:]:
difference = difference - num
return difference

print(subtrationDifference(myList))

With lowest possible time complexity , subtract every number in a list from the first lower number after it

I am not sure what complexity this exactly is but it is not O(n^2)you can try this. You can use a stack.

list1=[7,18,5,5,20,9,14,7,19]
stack = [(0, list1[0])]
res = [0] * len(list1)

for idx, i in enumerate(list1[1:], 1):
while stack and stack[-1][-1] > i:
old_idx, old_i = stack.pop()
res[old_idx] = old_i - i
stack.append((idx, i))

for idx, i in stack:
res[idx] = i

print(res)

Output

[2, 13, 5, 5, 11, 2, 7, 7, 19]

Benchmark using %%timeit -n 10 -r 10 in jupyter, input list has been increased to show the efficiency.

list1=[7,18,5,5,20,9,14,7,19]
for _ in range(10):
list1 += list1

This Solution

2.06 ms ± 37.1 µs per loop (mean ± std. dev. of 10 runs, 10 loops each)

Original Code

209 ms ± 2.71 ms per loop (mean ± std. dev. of 10 runs, 10 loops each)

This solution is faster.

Edit according to @JérômeRichard comment this runs in linear time O(n).

This is O(n). Here is the proof: you cannot append more element to the stack than the number of element in the input list because of the append. You cannot remove more element from the stack than the number of appended elements. Both pop and append are (amortized) O(1) operations. Then final loop is running in O(n)

Commented Version for Better Understanding

list1=[7,18,5,5,20,9,14,7,19] # input list

# create an unbounded stack
# `stack` will have a tuple of elements in the form (index, value)
# add the first element in the input list to the stack along with the index
stack = [(0, list1[0])] # (index, value)

# result list to store the result
# size will be the same as that of the input list `list1`
res = [0] * len(list1)

# start iterating the rest of the elements `list1[1:]` as the first element is already in the stack
# start enumerate index from 1
for idx, i in enumerate(list1[1:], 1):
"""
how the while loop works
check if stack is not empty and if the top element in the stack `stack[-1][-1]` is greater than the current element `i`
if this condition is `True`, it essentially means that `i` is the right most minimum element for the top element in the stack
stack[-1] is the top element of the stack, so `stack[-1][-1]` will give the value of the top element in the stack
remember every element in `stack` is a tuple of form (index, value), the value `stack[-1][-1]` is needed to compare, index `stack[-1][0]` is used later
both these checks are constant time operations `O(1)`.
"""
while stack and stack[-1][-1] > i:
# pop the top element and get the old index and old value as `old_idx` and `old_i`
old_idx, old_i = stack.pop()
# subtract old element `old_i` with current element `i`
# `old_idx` is the index where the result of the above operation must be placed
# assign the result to the `old_idx`
res[old_idx] = old_i - i

# add the current element and its index in the stack for comparing that in next iteration
stack.append((idx, i))

# if there are any elements remaining in the stack then these elements do not have a minimum value to their right
# so just get their index and set the corresponding value in the `res` list, that will be their position
for idx, i in stack:
res[idx] = i

# result :)
print(res)

Python list subtraction operation

Use a list comprehension:

[item for item in x if item not in y]

If you want to use the - infix syntax, you can just do:

class MyList(list):
def __init__(self, *args):
super(MyList, self).__init__(args)

def __sub__(self, other):
return self.__class__(*[item for item in self if item not in other])

you can then use it like:

x = MyList(1, 2, 3, 4)
y = MyList(2, 5, 2)
z = x - y

But if you don't absolutely need list properties (for example, ordering), just use sets as the other answers recommend.

How to add/subtract from elements in a list of integers

something like this

mylist = ["0", "1", "2"]
ints = [int(item)+1 for item in mylist]

print(ints)

How to subtract every item of a list

You can take the first number, then subtract the sum of the remaining numbers.

>>> x[0] - sum(x[1:])
60


Related Topics



Leave a reply



Submit