I want to loop through a list
and separate the current element from others.
Like this :
for e in the_list:
function_call(e, <the_list but e>)
Is there an elegant way to do that ?
You could use enumerate
and slice the list:
for index, elem in enumerate(the_list):
function_call(elem, the_list[:index] + the_list[index + 1:])
A nice solution that reads (reasonably) well and doesn't need messing around with indices.
>>> from itertools import combinations
>>> data = [1, 2, 3, 4]
>>> for item, rest in zip(data,
reversed(list(combinations(data, len(data)-1)))):
... print(item, rest)
...
1 (2, 3, 4)
2 (1, 3, 4)
3 (1, 2, 4)
4 (1, 2, 3)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With