Professional Documents
Culture Documents
4.3. Dicts - Python Notes (0.14.0)
4.3. Dicts - Python Notes (0.14.0)
0)
4.3. Dicts
SEARCH
Contents
Dicts Go
Quick example Enter search terms
Methods to query information or a module, class or
Methods to create new dictionary function name.
iterators This page: Show
Views source.
comparison TABLE OF
CONTENTS
1. Quick Start /
4.3.1. Quick example Tutorial
A dictionary is a sequence of items. Each item is a pair
2. Variables,
made of a key and a value. Dictionaries are not sorted.
expressions,
You can access to the list of keys or values
statements,
independently.
types
>>> d = {'first':'string value', 'second':[1,2]} 1. Iterators
>>> d.keys()
['first', 'second'] 2. Generators
>>> d.values()
['string value', [1, 2]] 3. Exceptions
You can access to the value of a given key as follows: 4. Decorators
>>> d['first'] 5. Classes
'string value'
6. Namespace and
Warning
scoping rules
You can not have duplicate keys in a dictionary
7. Packaging
Warning
8. Notes about
dictionary have no concept of order among elements sorting lists and
dictionaries
particular order:
https://thomas-cokelaer.info/tutorials/python/dicts.html 1/4
2021/5/13 4.3. Dicts — Python Notes (0.14.0)
>>> d.has_key('first')
True
SEARCH
you should be careful when using the affectation sign: logical operators
10. Notes on
>>> d1 = {'a': [1,2]}
>>> d2 = d1
>>> d2['a'] = [1,2,3,4]
>>> d1['a]
[1,2,3,4]
https://thomas-cokelaer.info/tutorials/python/dicts.html 2/4
2021/5/13 4.3. Dicts — Python Notes (0.14.0)
>>> d2 = d1.copy()
See also
copy.deepcopy()
SEARCH
You can clear a dictionary (i.e., remove all its items)
using the clear() method: Go
Enter search terms
>>> d2.clear() or a module, class or
{}
function name.
The clear() method deletes all items whereas del() This page: Show
deletes just one: source.
>>> d = {'a':1, 'b':2, 'c':3} TABLE OF
>>> del d['a'] CONTENTS
>>> d.clear()
1. Quick Start /
Create a new item with default value (if not provided, Tutorial
None is the default):
2. Variables,
>>> d2.setdefault('third', '') expressions,
>>> d2['third']
statements,
''
types
Create a dictionary given a set of keys:
1. Iterators
>>> d2.fromkeys(['first', 'second'])
2. Generators
another syntax is to start from an empty dictionary:
3. Exceptions
>>> {}.fromkeys(['first', 'second'])
{'first': None, 'second': None} 4. Decorators
Given 2 dictionaries d1 and d2, you can add all pairs of 8. Notes about
key/value from d2 into d1 by using the update method sorting lists and
(instead of looping and assigning each pair yourself: dictionaries
https://thomas-cokelaer.info/tutorials/python/dicts.html 3/4
2021/5/13 4.3. Dicts — Python Notes (0.14.0)
4.3.4. iterators
Dictionary provides iterators over values, keys or
items:
2. Variables,
4.3.6. comparison expressions,
you can compare dictionaries! Python first compares statements,
the sorted key-value pairs. It first sorts dictionaries by types
key and comapre their initial items. If the items hae 1. Iterators
different values, Python figures out the comparison
between them. Otherwise, the second items are 2. Generators
compared and so on. 3. Exceptions
4. Decorators
5. Classes
6. Namespace and
scoping rules
7. Packaging
8. Notes about
sorting lists and
dictionaries
9. Notes about
booleans and
logical operators
10. Notes on
https://thomas-cokelaer.info/tutorials/python/dicts.html 4/4