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]