1

How to remove one element from list while iterate

list is below

A = [ 1, 2, 3, 4,5,6]

Expected Output = [ 1, 2, 3, 4,5], [ 1, 2, 3, 4,6], [ 1, 2, 3, 5,6],[ 1, 2, 4,5,6],[ 1, 3, 4,5,6], [ 2, 3, 4,5,6]

  • Each time i will remove one element

code

for each in range(len(A)): 
    print(A.pop())

it will be removing every element till last iteration

Do i need to do while loop for this? while doing while where I should break?

Submitted September 19th 2021 by Admin

Answers
0

Because in the comment you say need solution without iterator I send this answer. If you want use iterator you can read this thread and find all combination.

You need to create a copy from A with [:] in the first of loop. then the base value in A you remove from copy and add copy to out.

Try this:

A = [1,2,3,4,5,6] out = []
for a in A: B = A[:] B.remove(a) out.append(B)

Admin | 4 weeks ago



Relevant Questions