List of Key Value Lists

I have a comma delimited string and then a space:

'gene_id EFNB2; Gene_type cDNA_supported; transcript_id EFNB2.aAug10; product_id EFNB2.aAug10;' 

I want to create a dictionary in one line, dividing it into separators, but for now I can only go to the list of lists:

 filter(None,[x.split() for x in atts.split(';')]) 

What gives me:

 [['gene_id', 'EFNB2'], ['Gene_type', 'cDNA_supported'], ['transcript_id', 'EFNB2.aAug10'], ['product_id', 'EFNB2.aAug10']] 

When I want:

 {'gene_id': 'EFNB2', 'Gene_type': 'cDNA_supported', 'transcript_id': 'EFNB2.aAug10', 'product_id': 'EFNB2.aAug10'} 

I tried:

 filter(None,{k:v for k,v in x.split() for x in atts.split(';')}) 

but it does not give anything. Does anyone know how to do this?

+6
source share
1 answer

Now you are very close, you can just call dict on your list of lists:

 >>> lst = [['gene_id', 'EFNB2'], ['Gene_type', 'cDNA_supported'], ['transcript_id', 'EFNB2.aAug10'], ['product_id', 'EFNB2.aAug10']] >>> dict(lst) {'Gene_type': 'cDNA_supported', 'gene_id': 'EFNB2', 'product_id': 'EFNB2.aAug10', 'transcript_id': 'EFNB2.aAug10'} 
+4
source

All Articles