Add 1 to all elements in list python

Many of the answers above are very good. I've also seen some weird answers that will do the job. Also, the last answer seen was through a normal loop. This willingness to give answers leads me to itertools and numpy, which will do the same job in a different way.

Here I present different ways to do the job, not answered above.

import operator
import itertools

x = [3, 5, 6, 7]

integer = 89

"""
Want more vairaint can also use zip_longest from itertools instead just zip
"""
#lazy eval
a = itertools.starmap[operator.add, zip[x, [89] * len[x]]] # this is not subscriptable but iterable
print[a]
for i in a:
  print[i, end = ","]


# prepared list
a = list[itertools.starmap[operator.add, zip[x, [89] * len[x]]]] # this returns list
print[a]



# With numpy [before this, install numpy if not present with `pip install numpy`]
import numpy

res = numpy.ones[len[x], dtype=int] * integer + x # it returns numpy array
res = numpy.array[x] + integer # you can also use this, infact there are many ways to play around
print[res]
print[res.shape] # prints structure of array, i.e. shape

# if you specifically want a list, then use tolist

res_list = res.tolist[]
print[res_list]

Output

>>>  # output by lazy val
>>> 92,94,95,96,                                     # output of iterating above starmap object
>>> [92, 94, 95, 96]                                 # output obtained by casting to list
>>>                   __
>>> # |\ | |  | |\/| |__| \ /
>>> # | \| |__| |  | |     |
>>> [92 94 95 96]                                    # this is numpy.ndarray object
>>> [4,]                                             # shape of array
>>> [92, 94, 95, 96]                                 # this is a list object [doesn't have a shape]

My sole reason to highlight the use of numpy is that one should always do such manipulations with libraries like numpy because it is performance efficient for very large arrays.

While working with the Python lists, we can come over a situation in which we require to add the integer k to each element in the list. We possibly need to iterate and add k to each element but that would increase the line of code. Let’s discuss certain shorthands to perform this task.

Method #1 : Using List Comprehension
List comprehension is just the short way to perform the task we perform using the naive method. This is mainly useful to save time and also is best among others when it comes to readability of the code.

test_list = [4, 5, 6, 3, 9]

print ["The original list is : " + str[test_list]]

K = 4

res = [x + K for x in test_list]

print ["The list after adding K to each element : " +  str[res]]

Output :

The original list is : [4, 5, 6, 3, 9]
The list after adding K to each element : [8, 9, 10, 7, 13]

Method #2 : Using map[] + lambda
map function can be used to pair each element with the lambda function which performs the task of adding K to each element in the list.

test_list = [4, 5, 6, 3, 9]

print ["The original list is : " + str[test_list]]

K = 4

res = list[map[lambda x : x + K, test_list]]

print ["The list after adding K to each element : " +  str[res]]

Output :

The original list is : [4, 5, 6, 3, 9]
The list after adding K to each element : [8, 9, 10, 7, 13]

Method #3 : Using map[] + operator.add
This is similar to the above function but uses the operator.add to add each element to other element from the other list of K formed before applying the map function. It adds the similar index elements of list.

import operator

test_list = [4, 5, 6, 3, 9]

print ["The original list is : " + str[test_list]]

K_list = [4] * len[test_list]

res = list[map[operator.add, test_list, K_list]]

print ["The list after adding K to each element : " +  str[res]]

Output :

The original list is : [4, 5, 6, 3, 9]
The list after adding K to each element : [8, 9, 10, 7, 13]

How do you add a number to each element in a list in Python?

Using map[] and add[] In place of the lambda operator, we can also use the add method along with map. In the below example, we create another list which has same number of elements as the length of the list and it contains the number which needs to be added. Then we apply the map method.

Can you += a list in Python?

list. extend[list2] adds the elements in list2 to the end of the list. Using + or += on a list is similar to using extend[].

How do you add things to each element in a list?

We can also use + operator to concatenate multiple lists to create a new list..
append[] This function add the element to the end of the list. ... .
insert[] This function adds an element at the given index of the list. ... .
extend[] This function append iterable elements to the list. ... .
List Concatenation..

How do you add a number to all elements in an array?

Add a value to each element of an array using for Loop.
Import numpy library and create a numpy array..
Using a for loop and range[] method iterate the array..
Add the given number to the each element..
Print the array..

Chủ Đề