In this scenario, I got a list that most always contain six items. If the items contained are less that six, always move the last two items to the end and pad between with empty space.
For example this ['A', 'B', 'E', 'F'] list will becomes this: ['A', 'B', '', '', 'E', 'F']
The code:
# Shift last two elements of a list and pad in between with a '<empty string>'
input_list = ['A', 'B', 'E', 'F']
print('Original list: ', input_list)
last_two_elem = input_list[-2:] # Get last two elements
del input_list[-2:] # Delete last two elements
print(f'Last two elements of the list: {last_two_elem}, the lsit after removing last two elements: {input_list}')
# Pad the list to nth time...
pad_value = 4
input_list += [''] * (pad_value - len(input_list)) # for 6 len list...
print(f'The lsit after padding: {input_list}')
# Extend the list with the last two elements...
input_list.extend(last_two_elem)
print(f'The lsit after extending: {input_list}')
That is it!
No comments:
Post a Comment