Calculations on a Python list | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Calculations on a Python list

Hello, can I make calculations on a Python list : add or subtract some elements or even the sum of all the elements of a list please? Can I have an example. Thanks by advance.

11th Jul 2021, 10:20 AM
malbar3353
malbar3353 - avatar
13 Answers
+ 15
malbar3353 Yes you can do calculation and filtering in list with the help of map,filter,list comprehension, etc ways... Map: numbers1 = [1, 2, 3] numbers2 = [4, 5, 6] result = map(lambda x, y: x + y, numbers1, numbers2) print(list(result)) #output :- [5,7,9] Filter:- seq = [0, 1, 2, 3, 5, 8, 13] result = filter(lambda x: x % 2 != 0, seq) #filtering odd numbers from seq list print(list(result)) #output :- [1, 3, 5, 13]
11th Jul 2021, 10:41 AM
Pallavi Bhardwaj
Pallavi Bhardwaj - avatar
+ 10
Ervis Meta I know but malbar3353 only wants to know how to do simple calculations in lists....
11th Jul 2021, 11:46 AM
Pallavi Bhardwaj
Pallavi Bhardwaj - avatar
+ 7
malbar3353 Lamdas is similar to def() that create functions..... Sorry for tough examples you can also do calculation in list by :- Adding 2 to all elements of a list let's say lista:- lista = [1,5,7,9,5] for i in range(len(lista)-1): lista[i] += 2 print(lista) #output :- [3,7,9,11,7] For printing sum of all elements in list:- lista = [1,5,7,9,5] print(sum(lista)) #output :- 27
11th Jul 2021, 11:44 AM
Pallavi Bhardwaj
Pallavi Bhardwaj - avatar
+ 4
instead of using map() / filter() and lambdas, you can use list comprehension or even the sum() builtin function: evens = [ 2, 4, 6, 8 ] odds = [v-1 for v in evens] nums = [evens[i]*odds[i] for i in range(len(odds))] less42 = [v for v in nums if v<42] https://python-reference.readthedocs.io/en/latest/docs/comprehensions/list_comprehension.html https://www.w3schools.com/python/python_lists_comprehension.asp
11th Jul 2021, 3:42 PM
visph
visph - avatar
+ 3
Calvin Thomas sometimes it could be more advisable to use map/filter (to improve readability as most obvious example -- in that case, it would be advisable to use 'normal' function or lambdas assigned to variables rather than having lambdas inlined)... however, that's also a question of coding style / preference ^^
11th Jul 2021, 4:57 PM
visph
visph - avatar
+ 2
Calvin Thomas yes, me too, when possible without too much readability downside... in addition, list comprehension is slightly more efficient than calling functions ^^
11th Jul 2021, 5:22 PM
visph
visph - avatar
+ 1
Though example 😅😅 Pallavi Bhardwaj if I get it I need to use the keyword map to do calculation ? What the word lambda is used for ?
11th Jul 2021, 11:15 AM
malbar3353
malbar3353 - avatar
+ 1
The map() function is used to simplyfy aplying of a specific function over an iterable. I usually use it in this form : map( func, iter ) Where func represents a function and iter an iterable; ( list, dictionary, set, tuple, ect) As lambda defines an 'on-fly' function, it is more suitable for using simple operations over an iterable (list) like adding or simple concatenation. filter() function does the same, but instead of returning an transformed iterable, it returns an filtered one from conditions of the function: filter( func, iter ) Note : To save the results of those functions use list() function or list comprehesion : list(map(lambda x: x + 2, [1,2,3,4])) [ i for i in map(lambda x: x + 2, [1,2,3,4])]
11th Jul 2021, 11:37 AM
Ervis Meta
Ervis Meta - avatar
11th Jul 2021, 11:53 AM
malbar3353
malbar3353 - avatar
+ 1
lst = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10) #add something print([n + 3 for n in lst]) #sum of lst members print(sum(lst))
31st Jul 2021, 9:07 AM
Angela
Angela - avatar
0
visph What's the point of using filter and map if everything can be done with a simple list comprehension?
11th Jul 2021, 4:51 PM
Calvin Thomas
Calvin Thomas - avatar
0
visph Oh, now I get you. Thanks. I still prefer comprehension though.
11th Jul 2021, 5:03 PM
Calvin Thomas
Calvin Thomas - avatar
0
Thanks Angela 😁
31st Jul 2021, 10:11 AM
malbar3353
malbar3353 - avatar