patternpythonMinor
Initializing and populating a Python dictionary, key -> List
Viewed 0 times
initializingpopulatingpythonanddictionarylistkey
Problem
I have a simple task that comes up often my work. I need to create a dictionary for an id key to a list of data associated with that key. I'm wondering if there is a better way to do this than the one I am using. The pattern is so common that I feel there must be better solution.
While this solution is fairly concise and clear to me, I wonder if there is a better way.
I think that writing a custom function for this might be less readable although I'm open to suggestions.
value_list_dict = {}
for line in f:
line.strip()
rec = line.split("\t")
my_key = rec[0]
important_value = rec[5] #or what ever it is
# the repetitive pattern I find myself doing a lot of.
if my_key not in value_list_dict:
value_list_dict[my_key] = []
value_list_dict[my_key].append(important_value)While this solution is fairly concise and clear to me, I wonder if there is a better way.
I think that writing a custom function for this might be less readable although I'm open to suggestions.
Solution
line.strip() does not modify the variable, it returns a modified string. But you can chain it together with the split. I would also jchoose a better variable name than rec. As J.F.Sebastian suggested in the comments, you could go with items:for line in f:
items = line.strip().split('\t')Your code is the perfect place for a
collections.defaultdict. You can give it a type which it will use when the key is not defined. It makes implementing a counter a lot easier (just pass it int, whose default constructor returns an int with value 0) or, give it list and it will give you an empty list:from collections import defaultdict
value_dict = defaultdict(list)
with open("file.txt") as f:
for line in f:
items = line.strip().split('\t')
key, value = items[0], items[5]
value_dict[key].append(value)I also added the
with..as construct in there, in case you are not yet using it.In python 3.x I would use the extended iterable unpacking:
key, *other_vals, value = line.strip().split('\t')Code Snippets
for line in f:
items = line.strip().split('\t')from collections import defaultdict
value_dict = defaultdict(list)
with open("file.txt") as f:
for line in f:
items = line.strip().split('\t')
key, value = items[0], items[5]
value_dict[key].append(value)key, *other_vals, value = line.strip().split('\t')Context
StackExchange Code Review Q#139585, answer score: 5
Revisions (0)
No revisions yet.