Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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]]
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading