How to iterate and sum each element in a list with another to find a number?

Maverick

I was taking a programming challenge and this question encountered, I somehow got stuck with it. I was given a list: [10, 13, 15, 18, 20, 15] And I had to find if the sum of any element in this would provide a result 30 and output should be the count As we could see we have two possibilities, 10+20 = 30 and 15+15, so the count is 2. But how to do it, should it be using for loop or itertools or any slicing or functional solution?

Kasravnd

You can use itetools.combinations and a generator expression within sum :

>>> my_list=[10, 13, 15, 18, 20, 15]
>>> from itertools import combinations
>>> sum(i+j==30 for i,j in combinations(my_list,2))
2

And if you want that items too you can use a list comprehension :

>>> [(i,j) for i,j in combinations(my_list,2) if i+j==30]
[(10, 20), (15, 15)]

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to find the sum of each element in a list with each other element

From Dev

How to iterate each element in list with all the others

From Dev

How to make each element in a list another list in python

From Dev

How would I find the sum of each individual list in my array?

From Dev

How to find the array indexes for each element in another array in MATLAB?

From Dev

Sum third element of each list in a list

From Java

How to find if a list contains any element of another list in sqlite

From Dev

how to find items in list that their sum is less than or equal a number

From Java

How do I multiply each element in a list by a number?

From Dev

Sum Up for each element of Collection.find()

From Dev

Python: How to find the number of items in each point on scatterplot and produce list?

From Dev

How to iterate through a list, element by element

From Dev

How to iterate over each element with a class name

From Dev

how to iterate through each element in a matrix in r

From Dev

How to find the index of last element on list and add it to another

From Dev

Drools: how to iterate list and add to another list

From Dev

How to iterate through a list and sum members together

From Dev

Find number of occurence of each element Xpath 1.0

From Dev

Find number of occurence of each element Xpath 1.0

From Dev

find if any element of list is in another list

From Dev

Each element in a list repeated a different number of times

From Dev

Find how many times each number between N and M can be expressed as a sum of a pair of primes

From Dev

How to multiply each element of a dataframe by sum of element in each row in R?

From Dev

Round each number of list to most near number in another list

From Dev

How to iterate through list in number of elements at once?

From Dev

How can i use each element in one list as an index for another list?

From Dev

How to SUM() each row into another column

From Dev

How to iterate multi dimensional array and calculate sum for each row in php?

From Dev

How to iterate in a list to find names and values with linq

Related Related

  1. 1

    How to find the sum of each element in a list with each other element

  2. 2

    How to iterate each element in list with all the others

  3. 3

    How to make each element in a list another list in python

  4. 4

    How would I find the sum of each individual list in my array?

  5. 5

    How to find the array indexes for each element in another array in MATLAB?

  6. 6

    Sum third element of each list in a list

  7. 7

    How to find if a list contains any element of another list in sqlite

  8. 8

    how to find items in list that their sum is less than or equal a number

  9. 9

    How do I multiply each element in a list by a number?

  10. 10

    Sum Up for each element of Collection.find()

  11. 11

    Python: How to find the number of items in each point on scatterplot and produce list?

  12. 12

    How to iterate through a list, element by element

  13. 13

    How to iterate over each element with a class name

  14. 14

    how to iterate through each element in a matrix in r

  15. 15

    How to find the index of last element on list and add it to another

  16. 16

    Drools: how to iterate list and add to another list

  17. 17

    How to iterate through a list and sum members together

  18. 18

    Find number of occurence of each element Xpath 1.0

  19. 19

    Find number of occurence of each element Xpath 1.0

  20. 20

    find if any element of list is in another list

  21. 21

    Each element in a list repeated a different number of times

  22. 22

    Find how many times each number between N and M can be expressed as a sum of a pair of primes

  23. 23

    How to multiply each element of a dataframe by sum of element in each row in R?

  24. 24

    Round each number of list to most near number in another list

  25. 25

    How to iterate through list in number of elements at once?

  26. 26

    How can i use each element in one list as an index for another list?

  27. 27

    How to SUM() each row into another column

  28. 28

    How to iterate multi dimensional array and calculate sum for each row in php?

  29. 29

    How to iterate in a list to find names and values with linq

HotTag

Archive