-2

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.

khelwood
  • 55,782
  • 14
  • 81
  • 108
dbld
  • 998
  • 1
  • 7
  • 17

2 Answers2

2

Using slices:

for i in range(len(xs)):
    func(xs[:i] + xs[i + 1:])
Mateen Ulhaq
  • 24,552
  • 19
  • 101
  • 135
1

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)
Tomerikoo
  • 18,379
  • 16
  • 47
  • 61