user1539097
user1539097

Reputation: 109

Making a dictionary from a list of lists

I have been unable to figure this out, I think the problem might be in the way I am making the list of lists. Can anyone help out? Thanks!

My desired outcome is

codondict = {'A': ['GCT','GCC','GCA','GCG'], 'C': ['TGT','TGC'], &c

but what i get is:

{'A': 'A', 'C': 'C', &c.

Here's my terminal:

A=['GCT','GCC','GCA','GCG']

C=['TGT','TGC']

D=['GAT','GAC']

E=['GAA','GAG']

F=['TTT','TTC']

G=['GGT','GGC','GGA','GGG']

H=['CAT','CAC']

I=['ATT','ATC','ATA']

K=['AAA','AAG']

L=['TTA','TTG','CTT','CTC','CTA','CTG']

M=['ATG']

N=['AAT','AAC']

P=['CCT','CCC','CCA','CCG']

Q=['CAA','CAG']

R=['CGT','CGC','CGA','CGG','AGA','AGG']

S=['TCT','TCC','TCA','TCG','AGT','AGC']

T=['ACT','ACC','ACA','ACG']

V=['GTT','GTC','GTA','GTG']

W=['TGG']

Y=['TAT','TAC']

aminoacids=['A','C','D','E','F','G','H','I','K','L','M','N','P','Q','R','S','T','V','W','Y']

from collections import defaultdict

codondict=defaultdict(list)

for i in aminoacids:

... for j in i:(ALSO TRIED for j in list(i))

... ... codondict[i]=j ...

codondict

defaultdict(, {'A': 'A', 'C': 'C', 'E': 'E', 'D': 'D', 'G': 'G', 'F': 'F', 'I': 'I', 'H': 'H', 'K': 'K', 'M': 'M', 'L': 'L', 'N': 'N', 'Q': 'Q', 'P': 'P', 'S': 'S', 'R': 'R', 'T': 'T', 'W': 'W', 'V': 'V', 'Y': 'Y'})

Upvotes: 0

Views: 282

Answers (4)

H.D.
H.D.

Reputation: 4454

You can use globals() built-in too, and dict comprehension:

codondict = {k:globals()[k] for k in aminoacids}

it's better to rely on locals() instead of globals(), like stummjr's solution, but you can't do so with dict comprehension directly

codondict = dict([(k,locals()[k]) for k in aminoacids])

However you can do this:

loc = locals()
codondict = {k:loc[k] for k in aminoacids}

If you change dinamically your aminoacids list or the aminoacids assignments, it's better to use something lazier, like:

codondict = lambda: {k:globals()[k] for k in aminoacids}

with this last you can always use the updated dictionary, but it's now a callable, so use codondict()[x] instead of codondict[x] to get an actual dict. This way you can store the entire dict like hist = codondict() in case you need to compare different historical versions of codondict. That's small enough to be useful in interactive modes, but not recommended in bigger codes, though.

Upvotes: 0

monkut
monkut

Reputation: 43832

You can try this:

condondict= dict(A=['GCT','GCC','GCA','GCG'],
C=['TGT','TGC'],
D=['GAT','GAC'],
E=['GAA','GAG'],
F=['TTT','TTC'],
G=['GGT','GGC','GGA','GGG'],
H=['CAT','CAC'],
I=['ATT','ATC','ATA'],
K=['AAA','AAG'],
L=['TTA','TTG','CTT','CTC','CTA','CTG'],
M=['ATG'],
N=['AAT','AAC'],
P=['CCT','CCC','CCA','CCG'],
Q=['CAA','CAG'],
R=['CGT','CGC','CGA','CGG','AGA','AGG'],
S=['TCT','TCC','TCA','TCG','AGT','AGC'],
T=['ACT','ACC','ACA','ACG'],
V=['GTT','GTC','GTA','GTG'],
W=['TGG'],
Y=['TAT','TAC'])

The reason to use defaultdict() is to allow access/creation of dictionary values without causing a KeyError, or by-pass using the form:

if key not in mydict.keys():
    mydict[key] = []
mydict[key].append(something)

If your not creating new keys dynamically, you don't really need to use defaultdict().

Also if your keys already represent the aminoacids, you and just iterate over the keys themselves.

for aminoacid, sequence in condondict.iteritems():
    # do stuff with with data...

Upvotes: 3

Valdir Stumm Junior
Valdir Stumm Junior

Reputation: 4667

Another way to do what you need is using the locals() function, which returns a dictionary containing the whole set of variables of the local scope, with the variable names as the keys and its contents as values.

for i in aminoacids:
    codondict[i] = locals()[i]

So, you could get the A list, for example, using: locals()['A'].

Upvotes: 1

msw
msw

Reputation: 43487

That's kind of verbose, and is confusing the name of a variable 'A' with its value A. Keeping to what you've got:

aminoacids = { 'A': A, 'C': C, 'D': D ... }

should get you the dictionary you ask for:

{ 'A' : ['GCT', 'GCC', 'GCA', 'GCG'], 'C' : ['TGT', 'TGC'], ... }

where the order of keys 'A' and 'C' may not be what you get back because dictionaries are not ordered.

Upvotes: 0

Related Questions