Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

double .get().get() dict python

example_dict = {
  'key1': {
    'key2': 'test2',
    'key3': 'test3'
  }
}

example_dict.get('key1', {}).get('key2')

# This will return None if either key1 or key2 does not exist.

# Note that this could still raise an AttributeError if example_dict['key1'] exists but is not a dict (or a dict-like object with a get method). The try..except code you posted would raise a TypeError instead if example_dict['key1'] is unsubscriptable.

# Another difference is that the try...except short-circuits immediately after the first missing key. The chain of get calls does not.
Comment

PREVIOUS NEXT
Code Example
Python :: rezing images of entire dataset in python 
Python :: python nextcord bot slash command 
Python :: override the text in buttons django admin 
Python :: who is rishi smaran = "RISHI SMARAN IS A 12 YEAR OLD NAUGHTY KID WHO CREATED ME" 
Python :: how to recurse a function 
Python :: build spacy custom ner model stackoverflow 
Python :: matplotlib set y lim 
Python :: python index where true 
Python :: string to list in python comma 
Python :: how to change python version on linux 
Python :: how to change the favicon in flask 
Python :: button icon pyqt5 
Python :: number of rows or columns in numpy ndarray python 
Python :: for loop for multiple scatter plots 
Python :: pythons os module choose random file 
Python :: wap to draw the shape of hexagonn in python 
Python :: df invert sort index 
Python :: pandas filter non nan 
Python :: jupyter notebook attach image 
Python :: rename one dataframe column python 
Python :: put array over array in numpy 
Python :: programe to check if a is divisible 
Python :: how to know how much lines a file has using python 
Python :: Replace empty string and "records with only spaces" with npnan pandas 
Python :: how to rearrange list in python 
Python :: Running setup.py bdist_wheel for opencv-python: still running... 
Python :: print zip object python 
Python :: check os python 
Python :: bulk file name changer in python 
Python :: how to count post by category django 
ADD CONTENT
Topic
Content
Source link
Name
5+7 =