I want to do a job for each item in a list. But I got a problem.
I want this code:
list=[1,2,3]
for i in list:
func(list.removed(i))
to do this:
func([2,3])
func([1,3])
func([1,2])
How can I make it? Thanks.
I want to do a job for each item in a list. But I got a problem.
I want this code:
list=[1,2,3]
for i in list:
func(list.removed(i))
to do this:
func([2,3])
func([1,3])
func([1,2])
How can I make it? Thanks.
You can use itertools
for that, by taking all combinations
of the list of its size - 1:
import itertools
lst = [1, 2, 3, 4]
for comb in itertools.combinations(lst, len(lst)-1):
print(comb)
Gives:
(1, 2, 3)
(1, 2, 4)
(1, 3, 4)
(2, 3, 4)