Select Page

Example dict

flat_hash = {
  'key1': 'value1',
  'key2': 'value2'
}

nested_hash = {
  'rootkey': {
    'subkey1': 'value1'
  }
}

Print dictionary

print(flat_hash)
print(nested_hash)

Access element

flat_hash['key1']
nested_hash['rootkey']['subkey1']

Adding new data

flat_hash['newkey'] = 'newvalue'

Delete data

del nested_hash['rootkey']['subkey1']`

Check if key exists

if ‘key1’ in flat_hash:
     # do something

Iterate through keys

for key in flat_hash:
   print(key)

Iterate through key-value pair

for key, value in flat_hash.items():
  print(f""key: {key} value: {value}"")