May-04-2021, 04:52 AM
Hey all,
One of my revision practices involves creating a function that removes all strings that has the highest string length in a list.
Expected Output:
Do I have to convert the list to a string then use .find to find the index position of the element that meets the required length? And how would I make it that it finds all occurrences, not just the first one it finds.
One of my revision practices involves creating a function that removes all strings that has the highest string length in a list.
Expected Output:
words_list = ['fish', 'barrel', 'like', 'shooting', 'sand', 'bank'] print(remove_long_words(words_list)) ['fish', 'barrel', 'like', 'sand', 'bank']Code so far:
def remove_long_words(words_list): length_long = get_longest_string_length(words_list) for ele in words_list: if len(ele) == length_long: #??? words_list.pop(???) return words_listI first made a function that returns the length of the longest string in the list, then used a for loop to iterate through every element in the list, and from there used an if statement to see if the length of the element is equal to the longest string length. I'm having trouble going on from there, how do I use the .pop method to remove the right elements from the list?
Do I have to convert the list to a string then use .find to find the index position of the element that meets the required length? And how would I make it that it finds all occurrences, not just the first one it finds.