0

I often deal with heterogeneous datasets and I acquire them as dictionaries in my python routines. I usually face the problem that the key of the next entry I am going to add to the dictionary already exists. I was wondering if there exists a more "pythonic" way to do the following task: check whether the key exists and create/update the corresponding pair key-item of my dictionary

myDict = dict() for line in myDatasetFile: if int(line[-1]) in myDict.keys(): myDict[int(line[-1])].append([line[2],float(line[3])]) else: myDict[int(line[-1])] = [[line[2],float(line[3])]] 
1
  • 1
    The fashionable word is pythonic. Commented Mar 31, 2015 at 9:10

5 Answers 5

7

Use a defaultdict.

from collections import defaultdict d = defaultdict(list) # Every time you try to access the value of a key that isn't in the dict yet, # d will call list with no arguments (producing an empty list), # store the result as the new value, and give you that. for line in myDatasetFile: d[int(line[-1])].append([line[2],float(line[3])]) 

Also, never use thing in d.keys(). In Python 2, that will create a list of keys and iterate through it one item at a time to find the key instead of using a hash-based lookup. In Python 3, it's not quite as horrible, but it's still redundant and still slower than the right way, which is thing in d.

Sign up to request clarification or add additional context in comments.

Comments

3

Its what that dict.setdefault is for.

setdefault(key[, default])

If key is in the dictionary, return its value. If not, insert key with a value of default and return default. default defaults to None.

example :

>>> d={} >>> d.setdefault('a',[]).append([1,2]) >>> d {'a': [[1, 2]]} 

Comments

1

Python follows the idea that it's easier to ask for forgiveness than permission.

so the true Pythonic way would be:

try: myDict[int(line[-1])].append([line[2],float(line[3])]) except KeyError: myDict[int(line[-1])] = [[line[2],float(line[3])]] 

for reference:

https://docs.python.org/2/glossary.html#term-eafp

https://stackoverflow.com/questions/6092992/why-is-it-easier-to-ask-forgiveness-than-permission-in-python-but-not-in-java

Comments

0

Try to catch the Exception when you get a KeyError

myDict = dict() for line in myDatasetFile: try: myDict[int(line[-1])].append([line[2],float(line[3])]) except KeyError: myDict[int(line[-1])] = [[line[2],float(line[3])]] 

Comments

-1

Or use:

myDict = dict() for line in myDatasetFile: myDict.setdefault(int(line[-1]),[]).append([line[2],float(line[3])]) 

1 Comment

Kasra beat you to setdefault by about three minutes. I would suggest deleting your answer as it adds nothing new.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.