0

How to compare 2 lists, element for element([0] with [0] etc) and add element to a third list if condition is met?

Comparing with <=, and both lists contain 7 elements (numbers).

26th Nov 2019, 12:55 PM
W. Khalid
5 Respuestas
+ 4
for x, y in zip(list1, list2): if x<=y: list3.append(x)
26th Nov 2019, 12:58 PM
HonFu
HonFu - avatar
+ 4
Yeah. The function zip combines two iterables (in this case lists) and groups them to pairs (or trios, quartets etc.) With x, y you unpack each of these little pair packages, and then you can compare them one by one. Important is that you try it with your own hands and test the result and see for yourself.
26th Nov 2019, 1:06 PM
HonFu
HonFu - avatar
+ 2
HonFu Thanks, will see if it works 👍🏻
26th Nov 2019, 1:07 PM
W. Khalid
+ 1
HonFu will it compare the list on a lndivual level? list1[0] with list2[0], list1[1] with list[2] and so forth?
26th Nov 2019, 1:02 PM
W. Khalid
+ 1
Yep
26th Nov 2019, 1:11 PM
Trigger
Trigger - avatar