Python Forum
Thread Rating:
  • 1 Vote(s) - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
2 Pairs 2 Lists
#8
There are only two numbers that add up to 24; 4 + 20, and they are both in the same list. You cannot add a number from list1 to a number from list2 to get 24. If you are just looking for two different numbers and the numbers can be from either list, the answer is 4 and 20

To solve the answer using one number from each list:
for a in list1:
    for b in list2:
        if a + b == 24:
            print(f'{a} + {b} = 24')
Tp solve for any combination of numbers in either list:
numbers = list1 + list2
for a in numbers:
    for b in numbers:
        if a + b == 24:
            print(f'{a} + {b} = 24')
This could be made more efficient if the lists were large or if you wanted to avoid double answers (4, 20 and 20, 4)
Reply


Messages In This Thread
2 Pairs 2 Lists - by Harshil - Aug-12-2020, 04:27 PM
RE: 2 Pairs 2 Lists - by DPaul - Aug-12-2020, 05:13 PM
RE: 2 Pairs 2 Lists - by Harshil - Aug-12-2020, 05:14 PM
RE: 2 Pairs 2 Lists - by DPaul - Aug-12-2020, 05:22 PM
RE: 2 Pairs 2 Lists - by Harshil - Aug-12-2020, 05:23 PM
RE: 2 Pairs 2 Lists - by DPaul - Aug-13-2020, 06:34 AM
RE: 2 Pairs 2 Lists - by DeaD_EyE - Aug-13-2020, 01:25 PM
RE: 2 Pairs 2 Lists - by deanhystad - Aug-13-2020, 02:32 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  how to find 'cycle' for key-value pairs in a dictionary? junnyfromthehood 1 3,725 Sep-29-2019, 01:07 AM
Last Post: ichabod801

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020