Python. How to sum up all even integers in a list?

前端 未结 4 2111
一向
一向 2021-01-07 04:45

I\'m completely new to the subject and I want to ask how to sum up all even integers in a list (without using functions (I haven\'t studied them yet))? For example:

4条回答
  •  感动是毒
    2021-01-07 05:18

    You can filter out all non-even elements like so

    my_list = [1, 3, 5, 6, 8, 10, 34, 2, 0, 3]
    even_list = filter(lambda x: x%2 == 0, my_list)
    

    and then sum the output like so:

    sum(even_list)
    

提交回复
热议问题