Python Forum
For Loop with List Comprehension
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
For Loop with List Comprehension
#6
(Dec-17-2020, 06:01 PM)muzikman Wrote: Actually, this won't work with x + anything greater than 1. So, I should have just hard coded the numbers in there, As such:
Nonsense
This will work for any number of lists, as long as you supply them as arguments to itertools.product
(Dec-17-2020, 05:57 PM)buran Wrote:
from itertools import product
list1 = [30, 50, 110, 40, 15, 75]
list2 = [10, 60, 20, 50]
sum_list = [item for item in product(list1, list2) if sum(item) > 100]
for item in sum_list:
    print(*item, sep=' ')

from itertools import product

list1 = [30, 50, 110, 40, 15, 75]
list2 = [10, 60, 20, 50]
list3 = [59, 68, 20, 78, 98]

sum_list = [item for item in product(list1, list2, list3) if sum(item) > 100]
for item in sum_list:
    print(*item, sep=' ')
Output:
30 10 68 30 10 78 30 10 98 30 60 59 30 60 68 30 60 20 30 60 78 30 60 98 30 20 59 30 20 68 30 20 78 30 20 98 30 50 59 30 50 68 30 50 78 30 50 98 50 10 59 50 10 68 50 10 78 50 10 98 50 60 59 50 60 68 50 60 20 50 60 78 50 60 98 50 20 59 50 20 68 50 20 78 50 20 98 50 50 59 50 50 68 50 50 20 50 50 78 50 50 98 110 10 59 110 10 68 110 10 20 110 10 78 110 10 98 110 60 59 110 60 68 110 60 20 110 60 78 110 60 98 110 20 59 110 20 68 110 20 20 110 20 78 110 20 98 110 50 59 110 50 68 110 50 20 110 50 78 110 50 98 40 10 59 40 10 68 40 10 78 40 10 98 40 60 59 40 60 68 40 60 20 40 60 78 40 60 98 40 20 59 40 20 68 40 20 78 40 20 98 40 50 59 40 50 68 40 50 20 40 50 78 40 50 98 15 10 78 15 10 98 15 60 59 15 60 68 15 60 78 15 60 98 15 20 68 15 20 78 15 20 98 15 50 59 15 50 68 15 50 78 15 50 98 75 10 59 75 10 68 75 10 20 75 10 78 75 10 98 75 60 59 75 60 68 75 60 20 75 60 78 75 60 98 75 20 59 75 20 68 75 20 20 75 20 78 75 20 98 75 50 59 75 50 68 75 50 20 75 50 78 75 50 98
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Messages In This Thread
For Loop with List Comprehension - by muzikman - Dec-17-2020, 05:39 PM
RE: For Loop with List Comprehension - by bowlofred - Dec-17-2020, 05:50 PM
RE: For Loop with List Comprehension - by buran - Dec-17-2020, 05:57 PM
RE: For Loop with List Comprehension - by muzikman - Dec-17-2020, 06:01 PM
RE: For Loop with List Comprehension - by buran - Dec-17-2020, 06:16 PM
RE: For Loop with List Comprehension - by muzikman - Dec-17-2020, 07:08 PM
RE: For Loop with List Comprehension - by muzikman - Dec-17-2020, 07:02 PM
RE: For Loop with List Comprehension - by bowlofred - Dec-17-2020, 08:33 PM
RE: For Loop with List Comprehension - by muzikman - Dec-17-2020, 11:14 PM
RE: For Loop with List Comprehension - by muzikman - Dec-17-2020, 07:10 PM
RE: For Loop with List Comprehension - by muzikman - Dec-17-2020, 07:16 PM
RE: For Loop with List Comprehension - by muzikman - Dec-17-2020, 07:20 PM
RE: For Loop with List Comprehension - by buran - Dec-17-2020, 07:28 PM
RE: For Loop with List Comprehension - by muzikman - Dec-17-2020, 07:31 PM
RE: For Loop with List Comprehension - by muzikman - Dec-17-2020, 08:00 PM
RE: For Loop with List Comprehension - by buran - Dec-17-2020, 08:16 PM
RE: For Loop with List Comprehension - by muzikman - Dec-17-2020, 11:44 PM
RE: For Loop with List Comprehension - by muzikman - Dec-17-2020, 11:46 PM
RE: For Loop with List Comprehension - by buran - Dec-17-2020, 09:23 PM
RE: For Loop with List Comprehension - by muzikman - Dec-17-2020, 11:12 PM
RE: For Loop with List Comprehension - by bowlofred - Dec-17-2020, 11:36 PM
RE: For Loop with List Comprehension - by muzikman - Dec-18-2020, 12:01 AM
RE: For Loop with List Comprehension - by buran - Dec-18-2020, 06:06 AM
RE: For Loop with List Comprehension - by muzikman - Dec-18-2020, 10:45 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  List Comprehension Issue johnywhy 5 614 Jan-14-2024, 07:58 AM
Last Post: Pedroski55
Question mypy unable to analyse types of tuple elements in a list comprehension tomciodev 1 520 Oct-17-2023, 09:46 AM
Last Post: tomciodev
  Using list comprehension with 'yield' in function tester_V 5 1,325 Apr-02-2023, 06:31 PM
Last Post: tester_V
  list comprehension 3lnyn0 4 1,478 Jul-12-2022, 09:49 AM
Last Post: DeaD_EyE
  List comprehension used differently coder_sw99 3 1,770 Oct-03-2021, 04:12 PM
Last Post: coder_sw99
  How to invoke a function with return statement in list comprehension? maiya 4 2,921 Jul-17-2021, 04:30 PM
Last Post: maiya
  List comprehension and Lambda cametan 2 2,284 Jun-08-2021, 08:29 AM
Last Post: cametan
  What is the difference between a generator and a list comprehension? Pedroski55 2 2,269 Jan-02-2021, 04:24 AM
Last Post: Pedroski55
  Using recursion instead of for loops / list comprehension Drone4four 4 3,199 Oct-10-2020, 05:53 AM
Last Post: ndc85430
  Appending to list of list in For loop nico_mnbl 2 2,399 Sep-25-2020, 04:09 PM
Last Post: nico_mnbl

Forum Jump:

User Panel Messages

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