This is a test version of Biostars. For the public version, visit https://www.biostars.org.
Help with BioPython manual example

I wish to index a multi-fasta file I have but I'm having problems getting the BioPython manual example to work.

From section 5.4.2 (pg 59). I have the required fasta file in my Python folder

The first few lines work just as they do in the manual:

>>> from Bio import SeqIO
>>> orchid_dict = SeqIO.index("ls_orchid.fasta", "fasta")
>>> len(orchid_dict)
94

But the next line does not. Here's what it returns

>>> orchid_dict.keys()
<dict_keyiterator object at 0x0000000002245F98>

Would someone be able to explain what might be going on and why the output isn't the same as in the manual?

biopython

3 answers

You're using python3 and in the update from 2.7 to 3, dict.keys() now returns an key iterator rather than a list of keynames. The code examples you are copying were written in python2.7, so you may see a few differences.

Nonetheless, list(orchid_dict.keys()) should give you the same result as in the example

Thanks! That did indeed give me output from the manual. Thanks to everyone else as well for helping.

keys() is giving you an iterator, not a list. I imagine this is a code change that's newer than the documentation you referenced, performed to make the library more efficient and aligned with Python 3 behavior (see here).

If you want to see the reference names in an interactive session, just type list(orchid_dict.keys()) and it will work as you expect. Otherwise, you can use the keys() iterator in your code in exactly the same way as you would before.

Are you using Python 3? I think the behaviour of the keys function has changed from Python 2 to Python 3.

Try:

list(orchid_dict.keys())

to get the old behaviour.

Log in to answer this question.