In the following list data:
attri_values = ['gene_id "scaffold_200002.1"', 'gene_version "1"', 'transcript_id "scaffold_200002.1"', 'transcript_version "1"', 'exon_number "2"', 'gene_source "jgi"', 'gene_biotype "protein_coding"', 'transcript_source "jgi"', 'transcript_biotype "protein_coding"', 'exon_id "scaffold_200002.1.exon2"', 'exon_version "1"']
say I want to apply a conditional expression:
gene_id = [x for x in attri_values if 'gene_id' in x]
# But, when there is not list values with 'gene_id' I would like to return NA (string type) but not an empty list, but I am being unsuccessful
gene_name = [x if 'gene_name' in x else 'NA' for x in attri_values]
print(gene_name) #gives me
['NA', 'NA', 'NA', 'NA', 'NA', 'NA', 'NA', 'NA', 'NA', 'NA', 'NA']
# I only want one 'NA' in string format.
I tried to write else conditions at different points. I also tried several examples from stackE but no success.
if else in a list comprehension
if/else in Python's list comprehension?
Thanks,