Python Forum
Can't seem to figure out how to put all of the lists items from a loop into 1 list
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Can't seem to figure out how to put all of the lists items from a loop into 1 list
#1
So I have this code:
Btw it uses BioPython

from Bio import SeqIO
import collections as C
import requests as R
import itertools

proteins2 = list(SeqIO.parse("2-proteins-tryout.gb","gb"))
#print(proteins2)

for protein in proteins2:
    #print(protein)
    for f in protein.features:
        if f.type == "BLAST":
            if "db_xref" in f.qualifiers:
                idss = f.qualifiers["db_xref"]
                print(idss)
                
            #Read file
            for idd in idss:
                
                fullProtein = SeqIO.read(f'{idd}.xml','uniprot-xml')
                #print(fullProtein)
        
                goTerms = []
                for ref in fullProtein.dbxrefs:
                    if ref.startswith('GO:'):
                        goTerm = ref[3:]
                        goTerms.append(goTerm)
                print(goTerms)
                
                allTerms = []
                for terms in goTerms:
                     allTerms.append(terms)
                print(allTerms)
For now it does what I want and gives me this results:

Quote:['P52326', 'P26480', 'P52327', 'P0A2E3', 'P00579', 'O24744', 'P32001', 'P57163', 'Q89B10', 'Q83BB6', 'Q9PDM9', 'Q87DT7', 'Q8PG33', 'P43766', 'Q8P4H2', 'P52325', 'P33452', 'Q59753', 'Q2K619', 'P52324', 'D5AQI9', 'P17531', 'P0CZ15', 'Q1RKH7', 'Q92FZ8', 'Q4UJT1', 'P33451', 'Q68VQ5']
['GO:0001123', 'GO:0003677', 'GO:0003700', 'GO:0005737', 'GO:0016987']
['GO:0001123', 'GO:0003677', 'GO:0003700', 'GO:0005737', 'GO:0016987']
['GO:0001123', 'GO:0003677', 'GO:0003700', 'GO:0005737', 'GO:0016987']
['GO:0001123', 'GO:0003677', 'GO:0003700', 'GO:0005737', 'GO:0016987']
['GO:0001123', 'GO:0003677', 'GO:0003700', 'GO:0005829', 'GO:0009408', 'GO:0016987']
['GO:0001123', 'GO:0003677', 'GO:0003700', 'GO:0005737', 'GO:0016987']
['GO:0001123', 'GO:0003677', 'GO:0003700', 'GO:0005737', 'GO:0016987']
['GO:0001123', 'GO:0003677', 'GO:0003700', 'GO:0005737', 'GO:0016987']
['GO:0001123', 'GO:0003677', 'GO:0003700', 'GO:0005737', 'GO:0016987']
['GO:0001123', 'GO:0003677', 'GO:0003700', 'GO:0005737', 'GO:0006352', 'GO:0016987']
['GO:0001123', 'GO:0003677', 'GO:0003700', 'GO:0005737', 'GO:0016987']
['GO:0001123', 'GO:0003677', 'GO:0003700', 'GO:0005737', 'GO:0016987']
['GO:0001123', 'GO:0003677', 'GO:0003700', 'GO:0005737', 'GO:0016987']
['GO:0001123', 'GO:0003677', 'GO:0003700', 'GO:0005737', 'GO:0016987']
['GO:0001123', 'GO:0003677', 'GO:0003700', 'GO:0005737', 'GO:0016987']
['GO:0001123', 'GO:0003677', 'GO:0003700', 'GO:0005737', 'GO:0016987']
['GO:0001123', 'GO:0003677', 'GO:0003700', 'GO:0005737', 'GO:0016987']
['GO:0001123', 'GO:0003677', 'GO:0003700', 'GO:0005737', 'GO:0016987']
['GO:0001123', 'GO:0003677', 'GO:0003700', 'GO:0005737', 'GO:0016987']
['GO:0001123', 'GO:0003677', 'GO:0003700', 'GO:0005737', 'GO:0016987']
['GO:0001123', 'GO:0003677', 'GO:0003700', 'GO:0005737', 'GO:0016987']
['GO:0001123', 'GO:0003677', 'GO:0003700', 'GO:0005737', 'GO:0016987', 'GO:0030435']
['GO:0001123', 'GO:0003677', 'GO:0003700', 'GO:0005737', 'GO:0016987']
['GO:0001123', 'GO:0003677', 'GO:0003700', 'GO:0005737', 'GO:0016987']
['GO:0001123', 'GO:0003677', 'GO:0003700', 'GO:0005737', 'GO:0016987']
['GO:0001123', 'GO:0003677', 'GO:0003700', 'GO:0005737', 'GO:0016987']
['GO:0001123', 'GO:0003677', 'GO:0003700', 'GO:0005737', 'GO:0016987']
['GO:0001123', 'GO:0003677', 'GO:0003700', 'GO:0005737', 'GO:0016987']
['GO:0001123', 'GO:0003677', 'GO:0003700', 'GO:0005737', 'GO:0016987']
['GO:0001123', 'GO:0003677', 'GO:0003700', 'GO:0005737', 'GO:0016987']
['GO:0001123', 'GO:0003677', 'GO:0003700', 'GO:0005737', 'GO:0016987']
['GO:0001123', 'GO:0003677', 'GO:0003700', 'GO:0005737', 'GO:0016987']
['GO:0001123', 'GO:0003677', 'GO:0003700', 'GO:0005829', 'GO:0009408', 'GO:0016987']
['GO:0001123', 'GO:0003677', 'GO:0003700', 'GO:0005737', 'GO:0016987']
['GO:0001123', 'GO:0003677', 'GO:0003700', 'GO:0005737', 'GO:0016987']
['GO:0001123', 'GO:0003677', 'GO:0003700', 'GO:0005737', 'GO:0016987']
['GO:0001123', 'GO:0003677', 'GO:0003700', 'GO:0005737', 'GO:0016987']
['GO:0001123', 'GO:0003677', 'GO:0003700', 'GO:0005737', 'GO:0006352', 'GO:0016987']
['GO:0001123', 'GO:0003677', 'GO:0003700', 'GO:0005737', 'GO:0016987']
['GO:0001123', 'GO:0003677', 'GO:0003700', 'GO:0005737', 'GO:0016987']
['GO:0001123', 'GO:0003677', 'GO:0003700', 'GO:0005737', 'GO:0016987']
['GO:0001123', 'GO:0003677', 'GO:0003700', 'GO:0005737', 'GO:0016987']
['GO:0001123', 'GO:0003677', 'GO:0003700', 'GO:0005737', 'GO:0016987']
['GO:0001123', 'GO:0003677', 'GO:0003700', 'GO:0005737', 'GO:0016987']
['GO:0001123', 'GO:0003677', 'GO:0003700', 'GO:0005737', 'GO:0016987']
['GO:0001123', 'GO:0003677', 'GO:0003700', 'GO:0005737', 'GO:0016987']
['GO:0001123', 'GO:0003677', 'GO:0003700', 'GO:0005737', 'GO:0016987']
['GO:0001123', 'GO:0003677', 'GO:0003700', 'GO:0005737', 'GO:0016987']
['GO:0001123', 'GO:0003677', 'GO:0003700', 'GO:0005737', 'GO:0016987']
['GO:0001123', 'GO:0003677', 'GO:0003700', 'GO:0005737', 'GO:0016987', 'GO:0030435']
['GO:0001123', 'GO:0003677', 'GO:0003700', 'GO:0005737', 'GO:0016987']
['GO:0001123', 'GO:0003677', 'GO:0003700', 'GO:0005737', 'GO:0016987']
['GO:0001123', 'GO:0003677', 'GO:0003700', 'GO:0005737', 'GO:0016987']
['GO:0001123', 'GO:0003677', 'GO:0003700', 'GO:0005737', 'GO:0016987']
['GO:0001123', 'GO:0003677', 'GO:0003700', 'GO:0005737', 'GO:0016987']
['GO:0001123', 'GO:0003677', 'GO:0003700', 'GO:0005737', 'GO:0016987']
['Q9RR29', 'D4GU70', 'P08075', 'A6VG23', 'A6UP85', 'A4FX98', 'Q58501', 'Q6LYB5', 'A6UUQ4', 'Q975F9', 'Q9Y725', 'Q6FRY2', 'P0C5I2', 'O93827', 'Q8BTZ7', 'Q9Y5P6', 'Q752H4', 'P41940', 'Q6BN12', 'O74624', 'Q2YDJ9', 'A2VD83', 'Q4U3E8']
['GO:0008879', 'GO:0017000', 'GO:0046872']
['GO:0006486', 'GO:0016779', 'GO:0045232', 'GO:0046872']
['GO:0008879', 'GO:0019872', 'GO:0046872']
['GO:0003977', 'GO:0006048', 'GO:0019134']
['GO:0003977', 'GO:0006048', 'GO:0019134']
['GO:0003977', 'GO:0006048', 'GO:0019134']
['GO:0003977', 'GO:0006048', 'GO:0019134']
['GO:0003977', 'GO:0006048', 'GO:0019134']
['GO:0003977', 'GO:0006048', 'GO:0019134']
['GO:0000166', 'GO:0003977', 'GO:0003983', 'GO:0006048', 'GO:0008879', 'GO:0019134']
['GO:0000032', 'GO:0004475', 'GO:0005525', 'GO:0005737', 'GO:0006486', 'GO:0007049', 'GO:0009298']
['GO:0004475', 'GO:0005525', 'GO:0005737', 'GO:0007049', 'GO:0009298']
['GO:0004475', 'GO:0005525', 'GO:0005737', 'GO:0009298']
['GO:0000032', 'GO:0004475', 'GO:0005525', 'GO:0005737', 'GO:0005886', 'GO:0007049', 'GO:0009298', 'GO:0009986', 'GO:0030445']
['GO:0004475', 'GO:0005525', 'GO:0005737', 'GO:0005739', 'GO:0009298']
['GO:0004475', 'GO:0005525', 'GO:0005737', 'GO:0009298']
['GO:0000032', 'GO:0004475', 'GO:0005525', 'GO:0005737', 'GO:0006486', 'GO:0007049', 'GO:0009272', 'GO:0009298', 'GO:0051286']
['GO:0000032', 'GO:0004475', 'GO:0005525', 'GO:0005737', 'GO:0006486', 'GO:0007049', 'GO:0009298']
['GO:0004475', 'GO:0005525', 'GO:0005737', 'GO:0007049', 'GO:0009272', 'GO:0009298', 'GO:0051286']
['GO:0004475', 'GO:0005525', 'GO:0005737', 'GO:0007049', 'GO:0009298']
['GO:0004475', 'GO:0005525', 'GO:0005737', 'GO:0009298']
['GO:0004475', 'GO:0005525', 'GO:0009298']
['GO:0004475', 'GO:0005525', 'GO:0005737', 'GO:0007049', 'GO:0009298', 'GO:0070590']
['P14738', 'Q2FE03', 'Q6G6H3', 'A7X6I5', 'Q6GDU5', 'Q2YW62', 'A0A0H2XKG3']
['GO:0001968', 'GO:0005576', 'GO:0005618', 'GO:0007155', 'GO:0009405', 'GO:0016020', 'GO:0070051', 'GO:0098630']
['GO:0005576', 'GO:0005618', 'GO:0007155', 'GO:0009405', 'GO:0016020']
['GO:0005576', 'GO:0005618', 'GO:0007155', 'GO:0009405', 'GO:0016020']
['GO:0005576', 'GO:0005618', 'GO:0007155', 'GO:0009405', 'GO:0016020']
['GO:0005576', 'GO:0005618', 'GO:0007155', 'GO:0009405', 'GO:0016020']
['GO:0005576', 'GO:0005618', 'GO:0007155', 'GO:0009405', 'GO:0016020']
['GO:0005576', 'GO:0005618', 'GO:0007155', 'GO:0009405', 'GO:0016020']
['P08320', 'P39347', 'P37317', 'P37326', 'P32053', 'P76542']
['GO:0003677', 'GO:0006310', 'GO:0015074', 'GO:0016740', 'GO:0016787', 'GO:0044826', 'GO:0046718', 'GO:0075713']
['GO:0003677', 'GO:0006310', 'GO:0015074', 'GO:0044826', 'GO:0046718', 'GO:0075713']
['GO:0003677', 'GO:0006310', 'GO:0015074', 'GO:0016740', 'GO:0016787', 'GO:0044826', 'GO:0046718', 'GO:0075713']
['GO:0006310', 'GO:0015074', 'GO:0032359', 'GO:0042802', 'GO:0043565', 'GO:0044826', 'GO:0046718', 'GO:0075713']
['GO:0003677', 'GO:0008979', 'GO:0032359', 'GO:0044826', 'GO:0046718']
['GO:0003677', 'GO:0006310', 'GO:0015074', 'GO:0044826', 'GO:0046718', 'GO:0075713']
['P69451', 'Q8XDR6', 'P63521', 'Q8ZES9', 'P46450', 'P94547', 'O07610', 'Q0DV32', 'P41636', 'Q42982', 'Q54P79', 'Q54P77', 'P14912', 'P14913', 'O24146', 'Q54P78']
['GO:0003996', 'GO:0004467', 'GO:0005504', 'GO:0005524', 'GO:0005737', 'GO:0005829', 'GO:0006629', 'GO:0006631', 'GO:0006635', 'GO:0006637', 'GO:0008654', 'GO:0009411', 'GO:0009898', 'GO:0015908', 'GO:0070538', 'GO:0102391']
['GO:0003996', 'GO:0004467', 'GO:0005524', 'GO:0016020', 'GO:0102391']
['GO:0003996', 'GO:0004467', 'GO:0005524', 'GO:0016020', 'GO:0102391']
['GO:0003996', 'GO:0004467', 'GO:0005524', 'GO:0016020', 'GO:0102391']
['GO:0003996', 'GO:0004467', 'GO:0005524', 'GO:0016020', 'GO:0102391']
['GO:0003996', 'GO:0004467', 'GO:0005524', 'GO:0102391']
['GO:0003996', 'GO:0004467', 'GO:0005524', 'GO:0006635', 'GO:0102391']
['GO:0004321', 'GO:0005524', 'GO:0005777', 'GO:0009695', 'GO:0016874']
['GO:0005524', 'GO:0009698', 'GO:0016207']
['GO:0005524', 'GO:0009698', 'GO:0010584', 'GO:0016207']
['GO:0005524', 'GO:0009698', 'GO:0016207']
['GO:0005524', 'GO:0009698', 'GO:0016207']
['GO:0005524', 'GO:0009698', 'GO:0016207']
['GO:0005524', 'GO:0009698', 'GO:0016207']
['GO:0005524', 'GO:0009698', 'GO:0016207']
['GO:0005524', 'GO:0009698', 'GO:0016207']

Everything that starts without GO: are just IDs xml {idd}.xml files and every fle has those GO: numbers. Basically (looking at the begging): for this ID 'P52326' we get this ['GO:0001123', 'GO:0003677', 'GO:0003700', 'GO:0005737', 'GO:0016987'], for the other ID 'P26480' we get the other GO: numbers ['GO:0001123', 'GO:0003677', 'GO:0003700', 'GO:0005737', 'GO:0016987'] and so.

What I am try to do is I want to put every GO: numbers into one list. So lets say this whole thing:
Quote:['GO:0001968', 'GO:0005576', 'GO:0005618', 'GO:0007155', 'GO:0009405', 'GO:0016020', 'GO:0070051', 'GO:0098630']
['GO:0005576', 'GO:0005618', 'GO:0007155', 'GO:0009405', 'GO:0016020']
['GO:0005576', 'GO:0005618', 'GO:0007155', 'GO:0009405', 'GO:0016020']
['GO:0005576', 'GO:0005618', 'GO:0007155', 'GO:0009405', 'GO:0016020']
['GO:0005576', 'GO:0005618', 'GO:0007155', 'GO:0009405', 'GO:0016020']
['GO:0005576', 'GO:0005618', 'GO:0007155', 'GO:0009405', 'GO:0016020']
['GO:0005576', 'GO:0005618', 'GO:0007155', 'GO:0009405', 'GO:0016020']

Into a list, like so:
['GO:0001968', 'GO:0005576', 'GO:0005618', 'GO:0007155', 'GO:0009405', 'GO:0016020', 'GO:0070051', 'GO:0098630','GO:0005576', 'GO:0005618', 'GO:0007155', 'GO:0009405', 'GO:0016020','GO:0005576', 'GO:0005618', 'GO:0007155', 'GO:0009405', 'GO:0016020','GO:0005576', 'GO:0005618', 'GO:0007155', 'GO:0009405', 'GO:0016020','GO:0005576', 'GO:0005618', 'GO:0007155', 'GO:0009405', 'GO:0016020','GO:0005576', 'GO:0005618', 'GO:0007155', 'GO:0009405', 'GO:0016020','GO:0005576', 'GO:0005618', 'GO:0007155', 'GO:0009405', 'GO:0016020']
so I try to iterate it and put into in a list:

allTerms = []
for terms in goTerms:
   allTerms.append(terms )
print(allTerms)
But it just gives me the same results as goTerms variable does.

I need the list, is because I need to find the duplicate GO: numbers in all of the ID's xml files. Like for example these IDs (or xml file) ['P52326', 'P26480', 'P52327', 'P0A2E3', 'P00579', 'O24744', 'P32001', 'P57163', 'Q89B10', 'Q83BB6', 'Q9PDM9', 'Q87DT7', 'Q8PG33', 'P43766', 'Q8P4H2', 'P52325', 'P33452', 'Q59753', 'Q2K619', 'P52324', 'D5AQI9', 'P17531', 'P0CZ15', 'Q1RKH7', 'Q92FZ8', 'Q4UJT1', 'P33451', 'Q68VQ5'] need to find the GO: numbers that are the same.
Reply
#2
Use the extend() method or list comprehension
allTerms = []
for terms in goTerms:
   allTerms.extend(terms )
print(allTerms)
or
allTerms = [t for terms in goTerms for t in terms]
Reply
#3
(Feb-21-2020, 01:43 PM)Gribouillis Wrote: Use the extend() method or list comprehension
allTerms = []
for terms in goTerms:
   allTerms.extend(terms )
print(allTerms)
or
allTerms = [t for terms in goTerms for t in terms]

It gives me this instead (its a snip it):
Quote:['G', 'O', ':', '0', '0', '0', '1', '1', '2', '3', 'G', 'O', ':', '0', '0', '0', '3', '6', '7', '7', 'G', 'O', ':', '0', '0', '0', '3', '7', '0', '0', 'G', 'O', ':', '0', '0', '0', '5', '7', '3', '7', 'G', 'O', ':', '0', '0', '1', '6', '9', '8', '7']
['G', 'O', ':', '0', '0', '0', '1', '1', '2', '3', 'G', 'O', ':', '0', '0', '0', '3', '6', '7', '7', 'G', 'O', ':', '0', '0', '0', '3', '7', '0', '0', 'G', 'O', ':', '0', '0', '0', '5', '7', '3', '7', 'G', 'O', ':', '0', '0', '1', '6', '9', '8', '7']
['G', 'O', ':', '0', '0', '0', '1', '1', '2', '3', 'G', 'O', ':', '0', '0', '0', '3', '6', '7', '7', 'G', 'O', ':', '0', '0', '0', '3', '7', '0', '0', 'G', 'O', ':', '0', '0', '0', '5', '7', '3', '7', 'G', 'O', ':', '0', '0', '1', '6', '9', '8', '7']
['G', 'O', ':', '0', '0', '0', '1', '1', '2', '3', 'G', 'O', ':', '0', '0', '0', '3', '6', '7', '7', 'G', 'O', ':', '0', '0', '0', '3', '7', '0', '0', 'G', 'O', ':', '0', '0', '0', '5', '7', '3', '7', 'G', 'O', ':', '0', '0', '1', '6', '9', '8', '7']
['G', 'O', ':', '0', '0', '0', '1', '1', '2', '3', 'G', 'O', ':', '0', '0', '0', '3', '6', '7', '7', 'G', 'O', ':', '0', '0', '0', '3', '7', '0', '0', 'G', 'O', ':', '0', '0', '0', '5', '8', '2', '9', 'G', 'O', ':', '0', '0', '0', '9', '4', '0', '8', 'G', 'O', ':', '0', '0', '1', '6', '9', '8', '7']
Reply
#4
I thought goTerms was a list of lists. What is it exactly?
Reply
#5
(Feb-21-2020, 02:26 PM)Gribouillis Wrote: I thought goTerms was a list of lists. What is it exactly?

Its a list of GO numbers for every ID in a list (['P52326', 'P26480', 'P52327', 'P0A2E3', 'P00579', 'O24744', 'P32001', 'P57163', 'Q89B10', 'Q83BB6', 'Q9PDM9', 'Q87DT7', 'Q8PG33', 'P43766', 'Q8P4H2', 'P52325', 'P33452', 'Q59753', 'Q2K619', 'P52324', 'D5AQI9', 'P17531', 'P0CZ15', 'Q1RKH7', 'Q92FZ8', 'Q4UJT1', 'P33451', 'Q68VQ5'])

I also checked the type:
print(type(goTerms))
<class 'list'>
<class 'list'>
<class 'list'>
....
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to parse and group hierarchical list items from an unindented string in Python? ann23fr 0 173 Mar-27-2024, 01:16 PM
Last Post: ann23fr
  Why do I have to repeat items in list slices in order to make this work? Pythonica 7 1,316 May-22-2023, 10:39 PM
Last Post: ICanIBB
  List all possibilities of a nested-list by flattened lists sparkt 1 911 Feb-23-2023, 02:21 PM
Last Post: sparkt
  Finding combinations of list of items (30 or so) LynnS 1 866 Jan-25-2023, 02:57 PM
Last Post: deanhystad
  user input values into list of lists tauros73 3 1,062 Dec-29-2022, 05:54 PM
Last Post: deanhystad
  returning a List of Lists nafshar 3 1,056 Oct-28-2022, 06:28 PM
Last Post: deanhystad
  Creating list of lists, with objects from lists sgrinderud 7 1,610 Oct-01-2022, 07:15 PM
Last Post: Skaperen
  For Word, Count in List (Counts.Items()) new_coder_231013 6 2,563 Jul-21-2022, 02:51 PM
Last Post: new_coder_231013
  How to get list of exactly 10 items? Mark17 1 2,480 May-26-2022, 01:37 PM
Last Post: Mark17
  how to assign items from a list to a dictionary CompleteNewb 3 1,561 Mar-19-2022, 01:25 AM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020