Of course this will generate each pair twice as each for loop will go through every item of the list.
You could use some itertools magic here to generate all possible combinations:
import itertools
for a, b in itertools.combinations(mylist, 2):
compare(a, b)
itertools.combinations will pair each element with each other element in the iterable, but only once.
You could still write this using index-based item access, equivalent to what you are used to, using nested for loops:
for i in range(len(mylist)):
for j in range(i + 1, len(mylist)):
compare(mylist[i], mylist[j])
Of course this may not look as nice and pythonic but sometimes this is still the easiest and most comprehensible solution, so you should not shy away from solving problems like that.
Answer from poke on Stack OverflowOf course this will generate each pair twice as each for loop will go through every item of the list.
You could use some itertools magic here to generate all possible combinations:
import itertools
for a, b in itertools.combinations(mylist, 2):
compare(a, b)
itertools.combinations will pair each element with each other element in the iterable, but only once.
You could still write this using index-based item access, equivalent to what you are used to, using nested for loops:
for i in range(len(mylist)):
for j in range(i + 1, len(mylist)):
compare(mylist[i], mylist[j])
Of course this may not look as nice and pythonic but sometimes this is still the easiest and most comprehensible solution, so you should not shy away from solving problems like that.
Use itertools.combinations(mylist, 2)
mylist = range(5)
for x,y in itertools.combinations(mylist, 2):
print x,y
0 1
0 2
0 3
0 4
1 2
1 3
1 4
2 3
2 4
3 4
comparing items in a list
loops - Compare the elements of a list in python - Stack Overflow
Compare list elements in python - Stack Overflow
Comparing all elements of a list with each other, without nested loops?
Hi All
I have a list and i want to compare if the each item is greater than the previous item but i'm getting stuck in my logic, this is what i have so far. Where am i going wrong please?
list2=[1,2,2,3,3,4,4,5]
for i in list2:
if i == list2[i-1]:
print(i)You are not iterating over the list by element (i.e. for el in a), that is a good thing because I believe modifying a list over which you are iterating wouldn't work.
However your approach still has a flaw, in the sense that a number of elements len(a) is calculated at the beginning of the loop and the index doesn't keep into account the fact that you are removing elements, so the inspected element will refer to the position in the list after the pop (skipping elements and exceeding list length).
Your example rewritten in a quite straightforward way using a temporary list b:
a=[1,3,3,6,3,5,5,7]
b=a[0:1]
for i in range(len(a)-1):
print (a[i],a[i+1])
if a[i]!=a[i+1]:
b.append(a[i+1])
a=b
Or a one line version:
from itertools import compress
list(compress(a,[x!=y for x,y in zip(a[:-1],a[1:])]))
Anyway if your purpose was to remove consecutive duplicate items in a list, you could easily search on google or on stack overflow 'python remove consecutive duplicates from a list'.
for this, next_one in zip(a, a[1:]):
compare(this, next_one)
One way is to take the maximum of the past values and compare it with the last:
>>> l = [1,2,3,4,5,6,7,8,9,10]
>>> if l[-1] > max(l[:-1]):
... print "It's bigger"
...
It's bigger
We iterate through the list except the last element and we check if all these elements are smaller than the last element.
print all(num < my_list[-1] for num in my_list[:-1])
In Python, the last element can be accessed with the negative indexing. So my_list[-1] refers to the last element and my_list[-2] refers to the last but one, etc.
Also we use slicing notation to exclude the last element from the list, with my_list[:-1].
Note: The advantage of this method is that, it short circuits immediately after finding a failure. So, we don't have to process the entire list always. In the best case, it returns after processing the first element. This is very useful if the list being processed is very big.
As Ashwini Chaudhary points out in the comments, this is the optimal way to do this
from itertools import islice
print all(num < my_list[-1] for num in islice(my_list, len(my_list) - 1))