-4

Hy i have a list of strings which look like:

atr = ['ID=cbs7435_mt', 'Name=cbs7435_mt', 'Dbxref=taxon:981350', 'Note=Pichia pastoris CBS 7435 mitochondrion%2C complete replicon sequence.', 'date=27-FEB-2015', 'mol_type=genomic DNA', 'organelle=mitochondrion', 'organism=Komagataella pastoris CBS 7435', 'strain=CBS 7435']

Now i want to create a dictionary which should look like:

my_dict = {'ID': 'cbs7435_mt', 'Name':'cbs7435_mt', ...}

Do someone has any advice how i could manage this? Thanks already!

rchaff
  • 9
  • 1
  • 1
  • 2

2 Answers2

3

Simply split it with = and use dict()

my_dict = dict(i.split('=') for i in atr)
akash karothiya
  • 5,736
  • 1
  • 19
  • 29
0

Use split() method of string to get Key and value for dictionary item.

Use for loop to iterate on given list input.

Demo:

>>> atr = ['ID=cbs7435_mt', 'Name=cbs7435_mt', 'Dbxref=taxon:981350']>>> result_dict = {}
>>> for item in atr:
...     key, value = item.split("=")
...     result_dict[key] = value
... 
>>> result_dict
{'Dbxref': 'taxon:981350', 'ID': 'cbs7435_mt', 'Name': 'cbs7435_mt'}
>>> 
Vivek Sable
  • 9,938
  • 3
  • 40
  • 56