programming:python:dictionary
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| programming:python:dictionary [2022/06/02 17:18] – levor | programming:python:dictionary [2022/06/06 18:29] (current) – levor | ||
|---|---|---|---|
| Line 2: | Line 2: | ||
| ^example^explanation / remarks^ | ^example^explanation / remarks^ | ||
| - | |d = {}| empty dictionary | | + | |d = {}|construct an empty dictionary | |
| - | |d = {" | + | |d = {" |
| - | |dict[" | + | |d[" |
| - | |v = dict[" | + | |v = d[" |
| - | |del dict[" | + | |
| |l = len(d)| get number of keys (which is the same as number of values)| | |l = len(d)| get number of keys (which is the same as number of values)| | ||
| |for key,val in d.items(): \\ ...| loop over all //key,val// pairs | | |for key,val in d.items(): \\ ...| loop over all //key,val// pairs | | ||
| - | |type(d)| <class ' | ||
| - | |cmp(d1, d2)| compares two dictionaries [need more info!]| | ||
| |d.clear()|delete all entries| | |d.clear()|delete all entries| | ||
| |d.copy()|returns a (shallow!) copy of the dictionary| | |d.copy()|returns a (shallow!) copy of the dictionary| | ||
| - | |d.get(key, default=None)|returns a value associated with a key. if key not found -> return // | + | |d.get(key, default=None)|returns a value associated with a //key//. if //key// not found -> return //default//| |
| - | |d.fromkeys()| | | + | |d.setdefault(key, |
| + | |d.fromkeys(seq, value=None)|// | ||
| |d.has_key(key)|returns a boolean value: True if key exists, else -> False| | |d.has_key(key)|returns a boolean value: True if key exists, else -> False| | ||
| - | |d.items()|a list of tuples: [(key1, | + | |d.keys()|returns a list of the keys: [key1, key2, ...]| |
| + | |d.values()|returns an **iterable** of the values| | ||
| + | |d**.items()**|returns | ||
| + | |d.update(d2)|add all d2 // | ||
| + | |del dict[" | ||
| + | |type(d)| <class ' | ||
| + | |cmp(d1, d2)| compares two dictionaries [need more info!]| | ||
programming/python/dictionary.1654190327.txt.gz · Last modified: 2022/06/02 17:18 by levor
