How to convert a list into list of lists in python?

I have a list of lists input_file = [[a,b,c],[1,2,3],[1,2,3]]
Then I have a piece of code that creates a new list of lists out of the fist list:

validated_list = []
    for value in input_file[0]:
        validated_list.append(value)
    for value in input_file[1:]:
        answer = value[2]
        if answer.isnumeric() == False:
            break
        elif int(answer) >= 1 and int(answer) <= 10:
           validated_list.append(value)     
    return validated_list

I expect to get the exact same list that i had in the beginning.
Instead, I am getting a list that looks like that:
[a,b,c,[1,2,3],[1,2,3]]

How do I make sure that the result looks like the original list of lists?

I tried adding the square brackets in the first append:

for value in input_file[0]:
        validated_list.append([value])

But what it returned was:
[[a],[b],[c],[1,2,3],[1,2,3]]

>Solution :

Solution:

input_file = [['a','b','c'],[1,2,3],[1,2,3]]

def validate_list(input_list):
    
    validated_list = []
    
    validated_list.append(input_list[0])

    for value in input_list[1:]:
        answer = value[2]
        if str(answer).isnumeric() == False:
            break

        elif int(answer) >= 1 and int(answer) <= 10:
            validated_list.append(value)  

    return validated_list

Output:

validate_list(input_file)
>>[['a', 'b', 'c'], [1, 2, 3], [1, 2, 3]]

Leave a Reply