Dec-17-2020, 05:39 PM
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.
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