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 loopSome 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.
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
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.
//ideone.com/8lHUit
This thread is archived
New comments cannot be posted and votes cannot be cast
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]