Home > Software design >  How do I save two lists to file, and then load them back inte the correct order?
How do I save two lists to file, and then load them back inte the correct order?

Time:10-12

Lets say I have the two lists:

 test_names = [['timothy', 'tim'],["clara"],["jerry", "jer", "j-dog"],]   
 test_numbers = ['123','234', '345',]   

I want to save them to a file, like this

123 ; timothy ; tim ;

234 ; clara ;

345 ; jerry ; jer ; j-dog ;

and then somehow return this file into the original lists above?

CodePudding user response:

It depends on what you mean by save to a file if you mean save to a python file you could just import it

from file import list
print(list)

Output

mia tim yoyo

However if you mean save it to a txt file unfortunately at the current moment I do not think this is possible if you know any way just comment and I will edit my answer

CodePudding user response:

I think something like this:

res = {}
for key in test_keys:
    for value in test_values:
        res[value] = key
        test_values.remove(value)
        break

with open("myfile.txt", 'w') as f: 
for key, value in res.items(): 
    f.write('%s;%s;\n' % (key, value))

Will result in something like this.

123;['timothy', 'tim'];

234;['clara'];

345;["jerry", "jer", "j-dog"];

  • Related