Sometimes a dictionary is nice for keeping things simple. You can directly access any value by looking at the key. But perhaps you need to split the dictionary up into two lists. Or go the other way; turn two related lists into a key-value dictionary.
Given a dictionary:
d = {}
d[a] = "1"
d[b] = "2"
d[c] = "3"
Split it into two lists:
alpha = [] numeric = []
for key, value in .items():
alpha.append(key)
numeric.append(value)
for key in d:
alpha.append(key)
numeric.append(d[key])
Going back to a dictionary is even easier.
newdict = dict(zip(alpha, numeric)
alpha = d.keys()
numeric = d.values()
No need for loops, man.
1:15 pm
Thanks for this.
For Dictionary to Lists, How about:
alpha, numeric = zip(*d.iteritems())