The question is published on by Tutorial Guruji team.
I am creating a dictionary as shown below and then adding values with given keys. But I am working with sequencing data, and some of the data will need to be entered multiple times, while other data should be entered only once. So currently I’m doing this:
my_dict = defaultdict(list) my_dict[seq].append(read_seq)
The dictionary then looks something like this:
{'TTTT': ['AGTG', 'AGGG'], 'AAAA': ['AGAG', 'TGTG']})
My problem is that I would like to associate two separate strings with each key but add them only upon the addition of a new key and then never again. And these should be different from the values shown above:
So i’m imagining something like this, thanks:
{'TTTT': ['AGTG', 'AGGG'], ['string1'], ['string2'], 'AAAA': ['AGAG', 'TGTG'], ['string3'], ['string4']})
Answer
How about holding a tuple or a dict as the value of your primary dict?
So instead of this(which cant quite exist in python):
{'TTTT': ['AGTG', 'AGGG'], ['string1'], ['string2'], 'AAAA': ['AGAG', 'TGTG'], ['string3'], ['string4']})
You will get:
{'TTTT': (['AGTG', 'AGGG'], ['string1'], ['string2']), 'AAAA': (['AGAG', 'TGTG'], ['string3'], ['string4'])}
This basically means a tuple of 3 elements for every key, the first one is your list and the second and third are your strings.
To implement that, define your defaultdict this way:
my_dict = defaultdict(lambda:([], [], []))
Then use my_dict[0]
for the original string list, my_dict[1]
and my_dict[2]
for your strings.
An example for appending a key/value to this dict:
my_dict[seq][0].append(read_seq) #And now your two strings: my_dict[seq][1] = 'string1' my_dict[seq][1] = 'string2'
If you don’t want to hold your strings in a list(which you probably shouldn’t), you can use a dict instead.Then it will look like:
{'TTTT': {'sequences':['AGTG', 'AGGG'], 'additional':('string1', 'string2')}, 'AAAA': {'sequences':['AGAG', 'TGTG'], 'additional':('string3', 'string4')}}
This will make adding key/values somewhat clearer:
my_dict = defaultdict(dict) #... my_dict[seq]['sequences'] = read_seq #And now your two strings: my_dict[seq]['additional'] = ('string1','string2')
I personally prefer the second option.