Browse Source

Dict, json

pull/3/head
Jure Šorn 7 years ago
parent
commit
c144dc129f
1 changed files with 8 additions and 1 deletions
  1. 9
      README.md

9
README.md

@ -52,6 +52,7 @@ Dictionary
```python ```python
collections.defaultdict(<type>) # Creates a dictionary with default values. collections.defaultdict(<type>) # Creates a dictionary with default values.
collections.OrderedDict() # Creates ordered dictionary.
dict(<list>) # Initiates a dict from list of key/value pairs. dict(<list>) # Initiates a dict from list of key/value pairs.
dict(zip(keys, values)) # Initiates a dict from two lists. dict(zip(keys, values)) # Initiates a dict from two lists.
{k: v for k, v in <dict>.items() if k in <list>} # Filters a dict by keys. {k: v for k, v in <dict>.items() if k in <list>} # Filters a dict by keys.
@ -528,7 +529,13 @@ import json
### Serialization ### Serialization
```python ```python
<str> = json.dumps(<object>, ensure_ascii=True, indent=None) <str> = json.dumps(<object>, ensure_ascii=True, indent=None)
<object> = json.loads(<str>)
<dict> = json.loads(<str>)
```
#### To preserve order:
```python
from collections import OrderedDict
<dict> = json.loads(<str>, object_pairs_hook=OrderedDict)
``` ```
### Read File ### Read File

Loading…
Cancel
Save