Unable to remove all the string components from the list, it just removes the first occurrence in the list

Viewed 34

Write a function that takes a list as the argument and returns a list with each element shifted left by one index and in case of string characters it needs to shift the characters to one space in the left as well. But the problem is the remove object doesnt remove more than one string object.Can anyone suggest the right way?

def shuffle_lst(inputs):
i=0
j=0
z=0
if isinstance(inputs,list)==False:
    raise Exception('The entered value should be a list, but the entered value is: {}'.format(my_string))
else:
    for i in range(0,len(inputs)):
        if isinstance(inputs[i],int)==True or isinstance(inputs[i],str)==True:
            final_str2=inputs[1:]+inputs[0:1]
            final_str2_copy=copy.deepcopy(final_str2)
            [final_str2.remove(j) for j in final_str2 if(type(j) is str)]
            print(final_str2)
            for z in range(0,len(final_str2_copy)):
                if isinstance(final_str2_copy[z],str)==True:
                    final_str=final_str2_copy[z][1:]+final_str2_copy[z][0:1]
                    final_str2.append(final_str)           
    print(final_str2)    
1 Answers

Your question as written seems to be somewhat confusing. Your statement of requirements clearly requires that the function take a list as it's argument. Your function fails this test.

See the following for a function which takes as input a list and produces the required shifting activities. In this method I make the following assumptions:

  1. The list entry shifted to the left is dropped from then output.
  2. Of the remaining list entries, if the entry is a string, the most left hand character is dropped.
  3. The function returns the modified list.
def shuffle_lst(mylst: list) -> list:
    for i in range(1, len(mylst)):
        if isinstance(mylst[i], str):
            mylst[i] = mylst[i][1:]
    return mylst[1:]  

Given

la = [1, 2, 'three', 'four', 5, 6]  

shuffle_lst(la)  

Yields:

[2, 'hree', 'our', 5, 6]
Related