Increase all elements in list python

Specific answer

With list comprehensions:

In [2]: list1 = [1,2,3,4,5,6]

In [3]: [x+170 for x in list1]
Out[3]: [171, 172, 173, 174, 175, 176]

With map:

In [5]: map(lambda x: x+170, list1)
Out[5]: [171, 172, 173, 174, 175, 176]

Turns out that the list comprehension is twice as fast:

$ python -m timeit 'list1=[1,2,3,4,5,6]' '[x+170 for x in list1]'
1000000 loops, best of 3: 0.793 usec per loop
$ python -m timeit 'list1=[1,2,3,4,5,6]' 'map(lambda x: x+170, list1)'
1000000 loops, best of 3: 1.74 usec per loop

Some bench-marking

After @mgilson posted the comment about numpy, I wondered how it stacked up. I found that for lists shorter than 50 or so elements, list comprehensions are faster, but numpy is faster beyond that.

Increase all elements in list python

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]

Posted by10 years ago

Archived

Increase all elements in list python

When I put a += in a for loop, the += doesn't actually get run. My goal is to lower each value in the list by one each loop, so long as it's greater than 0.

http://ideone.com/8lHUit

This thread is archived

New comments cannot be posted and votes cannot be cast

Increase all elements in list python

level 1

x -= 1 conceptually means "create a new number object that has the value one less than x and make x refer to that object." This does not affect the list at all, because the list is still referring to the old object.

Use a list comprehension or iterate by index. See this thread from just the other day.

level 1

the nice way would be to use a list comprehension.

list = [item-1 if item > 0 else item for item in list]

How do you increment an element in Python?

To increment a character in a Python, we have to convert it into an integer and add 1 to it and then cast the resultant integer to char.

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.

How do you increase a value in a list in Python?

Increment value of a key in Python dictionary.
Using if-else clause. A simple solution is to use the if-else clause with key in d operation. ... .
Using dict. get() function. ... .
Using dict. setdefault() function. ... .
Using try/except block. You can also use the d[key]=value syntax to increment a key in a dictionary. ... .
Using defaultdict..

How do you multiply all elements in a list Python?

Use the syntax [element * number for element in list] to multiply each element in list by number ..
a_list = [1, 2, 3].
multiplied_list = [element * 2 for element in a_list].
print(multiplied_list).