Python Forum
For Loop with List Comprehension
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
For Loop with List Comprehension
#1
Is this the easiest way to get the format below? I just hate hard coding the value x + 1, might as well just insert the digit 1. What if I didn't know the length,of the variable value, then what would I do?

BTW..This list comprehension is extremely powerful. It added each number in list1 to each number in list2. That's like 25 times.

list1 = [30, 50, 110, 40, 15, 75]
list2 = [10, 60, 20, 50]

sum_list = [(n1, n2) for n1 in list1 for n2 in list2 if n1 + n2 > 100]

for index, value in enumerate(sum_list):
    for x in range(len(value) - 1):
        print(value[x], value[x + 1])
OUTPUT:
50 60
110 10
110 60
110 20
110 50
75 60
75 50
Reply
#2
for x in sum_list:
    print(*x)
This for loop doesn't do anything:
for x in range(len(value) - 1):
print(value[x], value[x + 1])

# does same as
print(value[0], value[1])
Reply
#3
First of all, there's no need to explicitly count how many items there are and index into the list. Once you have your sum_list, you can just loop over the items to print them. (I'll show that below).

The other thing is if you need all the matchups from two lists, there's a function called product() that will generate them. Combining the two, you could do the above as:

from itertools import product

list1 = [30, 50, 110, 40, 15, 75]
list2 = [10, 60, 20, 50]

sum_list = [p for p in product(list1, list2) if sum(p) > 100]

for x, y in sum_list:
    print(x, y)

# or could do instead...
#for t in sum_list:
#    print(t[0], t[1])
Reply
#4
for item in sum_list:
    print(*item, sep=' ')
not sure why you use enumerate on line 5.
Also the loop on line 6 does not make sense at all and is not needed at all.

you can simplify the list comprehension using itertools.product

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=' ')
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
#5
Actually, this won't work with x + anything greater than 1. So, I should have just hard coded the numbers in there, As such:

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

sum_list = [(n1, n2, n3) for n1 in list1 for n2 in list2 for n3 in list3 if n1 + n2 + n3 > 100]

for index, value in enumerate(sum_list):
    for x in range(len(value) - 1):
        print(value[x], value[1], value[2])
Reply
#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
#7
Thank you for all the input. First of all, I am learning Python from scratch and can't use any of the functions such as product, etc. I need to do it with what I have learned thus far.

1. I agree, the second loop is not necessary. I went through it and debugged.
The reason I added the other loop is because I didn't truly understand that the sum_list had already taken care of it.
Here is the new code. I adjusted it according to "bowlofred" without the "product" function. What does this function do anyway?


list1 = [30, 50, 110, 40, 15, 75]
list2 = [10, 60, 20, 50, 80]


sum_list = [(n1, n2) for n1 in list1 for n2 in list2 if n1 + n2 > 100]

for index, value in sum_list:
        print(index, value)

OUTPUT:

30 80
50 60
50 80
110 10
110 60
110 20
110 50
110 80
40 80
75 60
75 50
75 80
Reply
#8
That is the way to do it for sure. Unlimited lists without changing a thing. Your print function takes an indefinite amount of params and separates them with a space. I like the way you did all of this.


Remember people: I am following a book and was just introduced to List Comprehensions. :)

This is great. The support on here is awesome.
Reply
#9
Buran:

That is the way it is done for sure. Your print function takes an indefinite amount of params and separates them by a space. You can add as many lists as you like and it would still work.

Remember People: I am following a book and just learned about List Comprehension today.

This place is great. All of these responses after an hour posting it.

Thank you all so much.
Reply
#10
I tried your code and it is much faster. I added a 4th list.

from itertools import product

list1 = [30, 50, 110, 40, 15, 75]
list2 = [10, 60, 20, 50]
list3 = [59, 68, 20, 78, 98]
list4 = [66, 77, 33, 22, 11]

sum_list = [item for item in product(list1, list2, list3, list4) if sum(item) > 100]
for item in sum_list:
    print(*item, sep=' ')
Output:
30 10 59 66 30 10 59 77 30 10 59 33 30 10 59 22 30 10 59 11 30 10 68 66 30 10 68 77 30 10 68 33 30 10 68 22 30 10 68 11 30 10 20 66 30 10 20 77 30 10 78 66 30 10 78 77 30 10 78 33 30 10 78 22 30 10 78 11 30 10 98 66 30 10 98 77 30 10 98 33 30 10 98 22 30 10 98 11 30 60 59 66 30 60 59 77 30 60 59 33 30 60 59 22 30 60 59 11 30 60 68 66 30 60 68 77 30 60 68 33 30 60 68 22 30 60 68 11 30 60 20 66 30 60 20 77 30 60 20 33 30 60 20 22 30 60 20 11 30 60 78 66 30 60 78 77 30 60 78 33 30 60 78 22 30 60 78 11 30 60 98 66 30 60 98 77 30 60 98 33 30 60 98 22 30 60 98 11 30 20 59 66 30 20 59 77 30 20 59 33 30 20 59 22 30 20 59 11 30 20 68 66 30 20 68 77 30 20 68 33 30 20 68 22 30 20 68 11 30 20 20 66 30 20 20 77 30 20 20 33 30 20 78 66 30 20 78 77 30 20 78 33 30 20 78 22 30 20 78 11 30 20 98 66 30 20 98 77 30 20 98 33 30 20 98 22 30 20 98 11 30 50 59 66 30 50 59 77 30 50 59 33 30 50 59 22 30 50 59 11 30 50 68 66 30 50 68 77 30 50 68 33 30 50 68 22 30 50 68 11 30 50 20 66 30 50 20 77 30 50 20 33 30 50 20 22 30 50 20 11 30 50 78 66 30 50 78 77 30 50 78 33 30 50 78 22 30 50 78 11 30 50 98 66 30 50 98 77 30 50 98 33 30 50 98 22 30 50 98 11 50 10 59 66 50 10 59 77 50 10 59 33 50 10 59 22 50 10 59 11 50 10 68 66 50 10 68 77 50 10 68 33 50 10 68 22 50 10 68 11 50 10 20 66 50 10 20 77 50 10 20 33 50 10 20 22 50 10 78 66 50 10 78 77 50 10 78 33 50 10 78 22 50 10 78 11 50 10 98 66 50 10 98 77 50 10 98 33 50 10 98 22 50 10 98 11 50 60 59 66 50 60 59 77 50 60 59 33 50 60 59 22 50 60 59 11 50 60 68 66 50 60 68 77 50 60 68 33 50 60 68 22 50 60 68 11 50 60 20 66 50 60 20 77 50 60 20 33 50 60 20 22 50 60 20 11 50 60 78 66 50 60 78 77 50 60 78 33 50 60 78 22 50 60 78 11 50 60 98 66 50 60 98 77 50 60 98 33 50 60 98 22 50 60 98 11 50 20 59 66 50 20 59 77 50 20 59 33 50 20 59 22 50 20 59 11 50 20 68 66 50 20 68 77 50 20 68 33 50 20 68 22 50 20 68 11 50 20 20 66 50 20 20 77 50 20 20 33 50 20 20 22 50 20 20 11 50 20 78 66 50 20 78 77 50 20 78 33 50 20 78 22 50 20 78 11 50 20 98 66 50 20 98 77 50 20 98 33 50 20 98 22 50 20 98 11 50 50 59 66 50 50 59 77 50 50 59 33 50 50 59 22 50 50 59 11 50 50 68 66 50 50 68 77 50 50 68 33 50 50 68 22 50 50 68 11 50 50 20 66 50 50 20 77 50 50 20 33 50 50 20 22 50 50 20 11 50 50 78 66 50 50 78 77 50 50 78 33 50 50 78 22 50 50 78 11 50 50 98 66 50 50 98 77 50 50 98 33 50 50 98 22 50 50 98 11 110 10 59 66 110 10 59 77 110 10 59 33 110 10 59 22 110 10 59 11 110 10 68 66 110 10 68 77 110 10 68 33 110 10 68 22 110 10 68 11 110 10 20 66 110 10 20 77 110 10 20 33 110 10 20 22 110 10 20 11 110 10 78 66 110 10 78 77 110 10 78 33 110 10 78 22 110 10 78 11 110 10 98 66 110 10 98 77 110 10 98 33 110 10 98 22 110 10 98 11 110 60 59 66 110 60 59 77 110 60 59 33 110 60 59 22 110 60 59 11 110 60 68 66 110 60 68 77 110 60 68 33 110 60 68 22 110 60 68 11 110 60 20 66 110 60 20 77 110 60 20 33 110 60 20 22 110 60 20 11 110 60 78 66 110 60 78 77 110 60 78 33 110 60 78 22 110 60 78 11 110 60 98 66 110 60 98 77 110 60 98 33 110 60 98 22 110 60 98 11 110 20 59 66 110 20 59 77 110 20 59 33 110 20 59 22 110 20 59 11 110 20 68 66 110 20 68 77 110 20 68 33 110 20 68 22 110 20 68 11 110 20 20 66 110 20 20 77 110 20 20 33 110 20 20 22 110 20 20 11 110 20 78 66 110 20 78 77 110 20 78 33 110 20 78 22 110 20 78 11 110 20 98 66 110 20 98 77 110 20 98 33 110 20 98 22 110 20 98 11 110 50 59 66 110 50 59 77 110 50 59 33 110 50 59 22 110 50 59 11 110 50 68 66 110 50 68 77 110 50 68 33 110 50 68 22 110 50 68 11 110 50 20 66 110 50 20 77 110 50 20 33 110 50 20 22 110 50 20 11 110 50 78 66 110 50 78 77 110 50 78 33 110 50 78 22 110 50 78 11 110 50 98 66 110 50 98 77 110 50 98 33 110 50 98 22 110 50 98 11 40 10 59 66 40 10 59 77 40 10 59 33 40 10 59 22 40 10 59 11 40 10 68 66 40 10 68 77 40 10 68 33 40 10 68 22 40 10 68 11 40 10 20 66 40 10 20 77 40 10 20 33 40 10 78 66 40 10 78 77 40 10 78 33 40 10 78 22 40 10 78 11 40 10 98 66 40 10 98 77 40 10 98 33 40 10 98 22 40 10 98 11 40 60 59 66 40 60 59 77 40 60 59 33 40 60 59 22 40 60 59 11 40 60 68 66 40 60 68 77 40 60 68 33 40 60 68 22 40 60 68 11 40 60 20 66 40 60 20 77 40 60 20 33 40 60 20 22 40 60 20 11 40 60 78 66 40 60 78 77 40 60 78 33 40 60 78 22 40 60 78 11 40 60 98 66 40 60 98 77 40 60 98 33 40 60 98 22 40 60 98 11 40 20 59 66 40 20 59 77 40 20 59 33 40 20 59 22 40 20 59 11 40 20 68 66 40 20 68 77 40 20 68 33 40 20 68 22 40 20 68 11 40 20 20 66 40 20 20 77 40 20 20 33 40 20 20 22 40 20 78 66 40 20 78 77 40 20 78 33 40 20 78 22 40 20 78 11 40 20 98 66 40 20 98 77 40 20 98 33 40 20 98 22 40 20 98 11 40 50 59 66 40 50 59 77 40 50 59 33 40 50 59 22 40 50 59 11 40 50 68 66 40 50 68 77 40 50 68 33 40 50 68 22 40 50 68 11 40 50 20 66 40 50 20 77 40 50 20 33 40 50 20 22 40 50 20 11 40 50 78 66 40 50 78 77 40 50 78 33 40 50 78 22 40 50 78 11 40 50 98 66 40 50 98 77 40 50 98 33 40 50 98 22 40 50 98 11 15 10 59 66 15 10 59 77 15 10 59 33 15 10 59 22 15 10 68 66 15 10 68 77 15 10 68 33 15 10 68 22 15 10 68 11 15 10 20 66 15 10 20 77 15 10 78 66 15 10 78 77 15 10 78 33 15 10 78 22 15 10 78 11 15 10 98 66 15 10 98 77 15 10 98 33 15 10 98 22 15 10 98 11 15 60 59 66 15 60 59 77 15 60 59 33 15 60 59 22 15 60 59 11 15 60 68 66 15 60 68 77 15 60 68 33 15 60 68 22 15 60 68 11 15 60 20 66 15 60 20 77 15 60 20 33 15 60 20 22 15 60 20 11 15 60 78 66 15 60 78 77 15 60 78 33 15 60 78 22 15 60 78 11 15 60 98 66 15 60 98 77 15 60 98 33 15 60 98 22 15 60 98 11 15 20 59 66 15 20 59 77 15 20 59 33 15 20 59 22 15 20 59 11 15 20 68 66 15 20 68 77 15 20 68 33 15 20 68 22 15 20 68 11 15 20 20 66 15 20 20 77 15 20 78 66 15 20 78 77 15 20 78 33 15 20 78 22 15 20 78 11 15 20 98 66 15 20 98 77 15 20 98 33 15 20 98 22 15 20 98 11 15 50 59 66 15 50 59 77 15 50 59 33 15 50 59 22 15 50 59 11 15 50 68 66 15 50 68 77 15 50 68 33 15 50 68 22 15 50 68 11 15 50 20 66 15 50 20 77 15 50 20 33 15 50 20 22 15 50 78 66 15 50 78 77 15 50 78 33 15 50 78 22 15 50 78 11 15 50 98 66 15 50 98 77 15 50 98 33 15 50 98 22 15 50 98 11 75 10 59 66 75 10 59 77 75 10 59 33 75 10 59 22 75 10 59 11 75 10 68 66 75 10 68 77 75 10 68 33 75 10 68 22 75 10 68 11 75 10 20 66 75 10 20 77 75 10 20 33 75 10 20 22 75 10 20 11 75 10 78 66 75 10 78 77 75 10 78 33 75 10 78 22 75 10 78 11 75 10 98 66 75 10 98 77 75 10 98 33 75 10 98 22 75 10 98 11 75 60 59 66 75 60 59 77 75 60 59 33 75 60 59 22 75 60 59 11 75 60 68 66 75 60 68 77 75 60 68 33 75 60 68 22 75 60 68 11 75 60 20 66 75 60 20 77 75 60 20 33 75 60 20 22 75 60 20 11 75 60 78 66 75 60 78 77 75 60 78 33 75 60 78 22 75 60 78 11 75 60 98 66 75 60 98 77 75 60 98 33 75 60 98 22 75 60 98 11 75 20 59 66 75 20 59 77 75 20 59 33 75 20 59 22 75 20 59 11 75 20 68 66 75 20 68 77 75 20 68 33 75 20 68 22 75 20 68 11 75 20 20 66 75 20 20 77 75 20 20 33 75 20 20 22 75 20 20 11 75 20 78 66 75 20 78 77 75 20 78 33 75 20 78 22 75 20 78 11 75 20 98 66 75 20 98 77 75 20 98 33 75 20 98 22 75 20 98 11 75 50 59 66 75 50 59 77 75 50 59 33 75 50 59 22 75 50 59 11 75 50 68 66 75 50 68 77 75 50 68 33 75 50 68 22 75 50 68 11 75 50 20 66 75 50 20 77 75 50 20 33 75 50 20 22 75 50 20 11 75 50 78 66 75 50 78 77 75 50 78 33 75 50 78 22 75 50 78 11 75 50 98 66 75 50 98 77 75 50 98 33 75 50 98 22 75 50 98 11
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  List Comprehension Issue johnywhy 5 440 Jan-14-2024, 07:58 AM
Last Post: Pedroski55
Question mypy unable to analyse types of tuple elements in a list comprehension tomciodev 1 427 Oct-17-2023, 09:46 AM
Last Post: tomciodev
  Using list comprehension with 'yield' in function tester_V 5 1,176 Apr-02-2023, 06:31 PM
Last Post: tester_V
  list comprehension 3lnyn0 4 1,360 Jul-12-2022, 09:49 AM
Last Post: DeaD_EyE
  List comprehension used differently coder_sw99 3 1,680 Oct-03-2021, 04:12 PM
Last Post: coder_sw99
  How to invoke a function with return statement in list comprehension? maiya 4 2,754 Jul-17-2021, 04:30 PM
Last Post: maiya
  List comprehension and Lambda cametan 2 2,201 Jun-08-2021, 08:29 AM
Last Post: cametan
  What is the difference between a generator and a list comprehension? Pedroski55 2 2,177 Jan-02-2021, 04:24 AM
Last Post: Pedroski55
  Using recursion instead of for loops / list comprehension Drone4four 4 3,073 Oct-10-2020, 05:53 AM
Last Post: ndc85430
  Appending to list of list in For loop nico_mnbl 2 2,331 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