How to Add an Integer to Each Element in a List

How to add a number to each element in list a that gives a new list b with sum added number

If you just want the final array:

def bw(n, k):
test_list = [1,2,3]
return [x+n*k for x in test_list]

print(bw(3, 2))

If you want a list of lists as well:

def bw(n, k):
test_list = [1,2,3]
res = []
for i in range(0,n):
test_list = [x+k for x in test_list]
res.append(test_list)

return test_list, res

print(bw(3, 2))

Call int() function on every list element?

This is what list comprehensions are for:

numbers = [ int(x) for x in numbers ]

Sum one number to every element in a list (or array) in Python

if you want to operate with list of numbers it is better to use NumPy arrays:

import numpy
a = [1, 1, 1 ,1, 1]
ar = numpy.array(a)
print ar + 2

gives

[3, 3, 3, 3, 3]

How do I add a number to every single element in python?

Using a list comprehension is a fast, compact way of getting the answer that you want. It's a useful tool for you to learn to write better Python.

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

def add_num_to_each(num, number_list)
return [ii + num for ii in number_list]

print(add_num_to_each(10, number_list))
>>> [11, 12, 13, 14, 15]

Adding an integer number to all elements of a list that are not integer

The core of your problem is that your list x contains strings representing floating-point numbers. You need to convert those strings to float objects.

More precisely:

  • Method 1 can be fixed by using dtype=float, as suggested on the comments:

    x = numpy.array(x, dtype=float)
    x_5 = x + 5
  • Method 2 can be fixed by converting the items of x to float values before adding 5:

    x_5 = [float(i) + 5 for i in x]
  • Method 3 can be fixed by using float instead of int, as your values are not integers but rather floating-point values:

    x_float = list(map(float, x))
    x_5 = [i + 5 for i in x_float]

    Note that this solution is equivalent to method 2, just a bit slower and more space consuming, as you are creating an additional list.

  • Method 4 can be fixed by removing the spurious x = numpy.array(x) line. You will end up with the same code as method 1.

  • As for method 5, I suspect that x is not the usual list, but rather it's a float object.

Other than converting values to the correct type, another thing you should try is to use different variable names for different things. In your code snippets, you're using x both for your lists/arrays and for the elements of those lists. While it's not always strictly required, using different variable names would solve a lot of confusion and save you from many headaches!

Add to integers in a list

Here is an example where the things to add come from a dictionary

>>> L = [0, 0, 0, 0]
>>> things_to_add = ({'idx':1, 'amount': 1}, {'idx': 2, 'amount': 1})
>>> for item in things_to_add:
... L[item['idx']] += item['amount']
...
>>> L
[0, 1, 1, 0]

Here is an example adding elements from another list

>>> L = [0, 0, 0, 0]
>>> things_to_add = [0, 1, 1, 0]
>>> for idx, amount in enumerate(things_to_add):
... L[idx] += amount
...
>>> L
[0, 1, 1, 0]

You could also achieve the above with a list comprehension and zip

L[:] = [sum(i) for i in zip(L, things_to_add)]

Here is an example adding from a list of tuples

>>> things_to_add = [(1, 1), (2, 1)]
>>> for idx, amount in things_to_add:
... L[idx] += amount
...
>>> L
[0, 1, 1, 0]

In Python, I want to add a number to each element in a list, but I get [[0,1],2] instead of [0,1,2]. How to fix this?

You need to unpack the items in b when creating the new list

listC.append([*b,i])

or if its easier to understand, add a new list with i to list b

listC.append(b + [i])

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)


Related Topics



Leave a reply



Submit