I would like to move more than one item from one list to another.
list1 = ['2D',' ',' ',' ',' ',' ',' ',' ',' ']
list2 = ['XX','XX','5D','4S','3D',' ',' ',' ',' ']
list3 = ['XX','XX','XX','8C','7H','6C',' ',' ',' ']
In the code above ' '
is a double space
I would like to be able to move '5D','4S','3D'
from list2
onto '8C','7H','6C'
in list3
.
I've tried the code below but it doesn't work.
list1 = ['2D',' ',' ',' ',' ',' ',' ',' ',' ']
list2 = ['XX','XX','5D','4S','3D',' ',' ',' ',' ']
list3 = ['XX','XX','XX','8C','7H','6C',' ',' ',' ']
items_to_be_moved = list2[list2.index('XX')+2 : list2.index(' ')]
list3[list3.index(' ')] = items_to_be_moved
del list2[list2.index('XX')+2 : list2.index(' ')]
print('list2',list2)
print('list3',list3)
and this returns
list2 ['XX', 'XX', ' ', ' ', ' ', ' ']
list3 ['XX', 'XX', 'XX', '8C', '7H', '6C', ['5D', '4S', '3D'], ' ', ' ']
However, i dont want to use list2.index('XX')+2
, i would like to use a code that gives the last index of 'XX'
just like list2.index(' ')
gives the first index of ' '
.
Also, i dont want the moved items to be in a separate list of their own within another list. For example: instead of returning
"list3 ['XX', 'XX', 'XX', '8C', '7H', '6C', ['5D', '4S', '3D'], ' ', ' ']"
the list
"list3 ['XX', 'XX', 'XX', '8C', '7H', '6C','5D', '4S', '3D', ' ', ' ']"
should be returned.