list.remove(x) x seems to be in list but not being found

Viewed 40

So in VSCode, I'm receiving this error...

enter image description here

Given the input

2

21 22

We have variables saying

best_choice = '22'

a: ['21', '22']

However, a.remove(best_choice) returns an error. I am completely confounded... please help. I checked for this problem on stacks and found that remove() doesn't work when iterating through a list, so I changed my loop to while(True). However, I'm still receiving the same error.

Here is the code:

import sys

def best_option(a):
    iterator = 0
    while(len(a)>1):
        previous_best_digit = 0
        current_best_digit = 0
        best_short_option = []
        return_list = []
        for item in a:
            if len(item)-1 < iterator:
                char_previous =  item[iterator-1:iterator]
                dig_previous = ord(char_previous)
                previous_best_digit = dig_previous
                best_short_option = item
                continue
            char = item[iterator:iterator+1]
            dig = ord(char)
            if dig > current_best_digit:
                current_best_digit = dig
                return_list.clear()
                return_list.append(item)
            elif dig == current_best_digit:
                return_list.append(item)
        if (current_best_digit < previous_best_digit):
            return_list.clear()
            return_list.append(best_short_option)
        a = return_list 
        iterator+=1
    return a


def largest_number(a):

    #write your code here
    res = ""
    while (True):
        best_choice = best_option(a) 
        print(best_choice)
        a.remove(best_choice)
        res += best_choice
        if (len(a)==1):
            break

    res.append(a)
    print(res)
    return res

if __name__ == '__main__':
    input = sys.stdin.read()
    data = input.split()
    a = data[1:]
    print(largest_number(a))
   

It's purpose create a string with the greatest possible value from a list of strings.

1 Answers

your best_choice is ['22'], not '22'

Related