This is a test version of Biostars. For the public version, visit https://www.biostars.org.
How to generate a fasta file from a python dictionary or lists?

I have two lists:

list_seq = [sequence1, sequence2, sequence3, sequence4]

list_name = [name1, name2, name3, name4]

I can zip them to a dictionary:

dict_s_n = dict(zip(list_seq & list_name))

How do I generate a fasta file from these two lists or the dictionary?

sequence fasta list dictionary python

This is in python, isn't it? I'm adding the tag and editing the title to make it more clear.

1 answer

You just need to write the name and sequence to a fasta format

>name1
sequence1
>name2
sequence2
..
..
..
ofile = open("my_fasta.txt", "w")

for i in range(len(list_seq)):
    ofile.write(">" + list_name[i] + "\n" +list_seq[i] + "\n")

#do not forget to close it
ofile.close()

Great! I didn't think it was that easy. Many thanks, tangming2005!

Hi ! I would just add ofile.close() to your solution, tangming2005, as you did not used the "with" statement to open "my_fasta.txt".

You are right! Do not forget to close ..

nice and simple. I wanted to do it with biopython, but this one is much easier.

Log in to answer this question.