Remove element, then iterate and combine elements in a list python

Viewed 125

Let say I have a list like this with n elements:

myList = ['cr','cd','cs','sp','sd','op','ol','fs','th','ag','ng','rt','bp']

I want to remove one element in the list every time while iterating then combine iteratively the other elements in my list to have an output like this:

# removing the first element, we would have:
['cd']
['cd', 'cs']
['cd', 'cs', 'sp']
['cd', 'cs', 'sp', 'sd']
['cd', 'cs', 'sp', 'sd', 'op']
['cd', 'cs', 'sp', 'sd', 'op', 'ol']
['cd', 'cs', 'sp', 'sd', 'op', 'ol', 'fs']
['cd', 'cs', 'sp', 'sd', 'op', 'ol', 'fs', 'th']
['cd', 'cs', 'sp', 'sd', 'op', 'ol', 'fs', 'th', 'ag']
['cd', 'cs', 'sp', 'sd', 'op', 'ol', 'fs', 'th', 'ag', 'ng']
['cd', 'cs', 'sp', 'sd', 'op', 'ol', 'fs', 'th', 'ag', 'ng', 'rt']
['cd', 'cs', 'sp', 'sd', 'op', 'ol', 'fs', 'th', 'ag', 'ng', 'rt', 'bp']

# Then removing the second element, we would have:

['cr']
['cr', 'cs']
['cr', 'cs', 'sp']
['cr', 'cs', 'sp', 'sd']
['cr', 'cs', 'sp', 'sd', 'op']
['cr', 'cs', 'sp', 'sd', 'op', 'ol']
['cr', 'cs', 'sp', 'sd', 'op', 'ol', 'fs']
['cr', 'cs', 'sp', 'sd', 'op', 'ol', 'fs', 'th']
['cr', 'cs', 'sp', 'sd', 'op', 'ol', 'fs', 'th', 'ag']
['cr', 'cs', 'sp', 'sd', 'op', 'ol', 'fs', 'th', 'ag', 'ng']
['cr', 'cs', 'sp', 'sd', 'op', 'ol', 'fs', 'th', 'ag', 'ng', 'rt']
['cr', 'cs', 'sp', 'sd', 'op', 'ol', 'fs', 'th', 'ag', 'ng', 'rt', 'bp']

# Removing ith element:
........

I have tried like this removing manually one element each time:

# 2nd element is removed
myList = ['cr','cs','sp','sd','op','ol','fs','th','ag','ng','rt','bp']
for i in range(1, len(myList) + 1): 
    input_f = myList[:i]
    print(input_f)

and get this result:

['cr']
['cr', 'cs']
['cr', 'cs', 'sp']
['cr', 'cs', 'sp', 'sd']
['cr', 'cs', 'sp', 'sd', 'op']
['cr', 'cs', 'sp', 'sd', 'op', 'ol']
['cr', 'cs', 'sp', 'sd', 'op', 'ol', 'fs']
['cr', 'cs', 'sp', 'sd', 'op', 'ol', 'fs', 'th']
['cr', 'cs', 'sp', 'sd', 'op', 'ol', 'fs', 'th', 'ag']
['cr', 'cs', 'sp', 'sd', 'op', 'ol', 'fs', 'th', 'ag', 'ng']
['cr', 'cs', 'sp', 'sd', 'op', 'ol', 'fs', 'th', 'ag', 'ng', 'rt']
['cr', 'cs', 'sp', 'sd', 'op', 'ol', 'fs', 'th', 'ag', 'ng', 'rt', 'bp']

This puts me one step closer but I can not figure out how to to handle the rest automatically. Any idea would be appreciated.

3 Answers
myList = ['a','b','c']

for index,elem in enumerate(myList):
    myList.pop(index)
    print(myList)
    myList.insert(index,elem)

You can try and solve it using list comprehension in your for loop:

a = ['a','b','c']
for i in a:
  print([[x for x in a if x != i][0]])
  print([x for x in a if x != i])

This outputs:

['b']
['b', 'c']
['a']
['a', 'c']
['a']
['a', 'b']

Your solution is quite close. You can take two list slices and combine them together to get the desired result at each iteration.

You can use nested loops to achieve the result you want for each removal of a list element.

The outer loop should iterate over the full range of the list.

The inner loop should iterate over the length of the modified list. Each iteration would print the first j elements out.

myList = ['cr','cd','cs','sp','sd','op','ol','fs','th','ag','ng','rt','bp']

for i in range(len(myList)):
    # these slices skip the i'th element at each iteration
    listSlice = myList[:i] + myList[i + 1:]

    for j in range(1, len(listSlice) + 1):
        print(listSlice[:j])

The output with your shown example would look like this:

['cd']
['cd', 'cs']
['cd', 'cs', 'sp']
['cd', 'cs', 'sp', 'sd']
['cd', 'cs', 'sp', 'sd', 'op']
['cd', 'cs', 'sp', 'sd', 'op', 'ol']
['cd', 'cs', 'sp', 'sd', 'op', 'ol', 'fs']
['cd', 'cs', 'sp', 'sd', 'op', 'ol', 'fs', 'th']
['cd', 'cs', 'sp', 'sd', 'op', 'ol', 'fs', 'th', 'ag']
['cd', 'cs', 'sp', 'sd', 'op', 'ol', 'fs', 'th', 'ag', 'ng']
['cd', 'cs', 'sp', 'sd', 'op', 'ol', 'fs', 'th', 'ag', 'ng', 'rt']
['cd', 'cs', 'sp', 'sd', 'op', 'ol', 'fs', 'th', 'ag', 'ng', 'rt', 'bp']
['cr']
['cr', 'cs']
['cr', 'cs', 'sp']
['cr', 'cs', 'sp', 'sd']
['cr', 'cs', 'sp', 'sd', 'op']
['cr', 'cs', 'sp', 'sd', 'op', 'ol']
['cr', 'cs', 'sp', 'sd', 'op', 'ol', 'fs']
['cr', 'cs', 'sp', 'sd', 'op', 'ol', 'fs', 'th']
['cr', 'cs', 'sp', 'sd', 'op', 'ol', 'fs', 'th', 'ag']
['cr', 'cs', 'sp', 'sd', 'op', 'ol', 'fs', 'th', 'ag', 'ng']
['cr', 'cs', 'sp', 'sd', 'op', 'ol', 'fs', 'th', 'ag', 'ng', 'rt']
['cr', 'cs', 'sp', 'sd', 'op', 'ol', 'fs', 'th', 'ag', 'ng', 'rt', 'bp']
...
Related