Python tutorial
Dictionary is a mapped data structure, which has key:value pair as an elements, key is immutable object, values can be mutable
method | description |
clear | removes all elements |
copy | does shallow-copy |
fromkeys | creates a new dictionary object, with keys from iterable object, value for each key is default |
get | gets value for a given key |
items | returns set, where each element in the set is a tuple,each tuple has key and value pair |
keys | returns keys |
pop | removes element for a given key |
popitem | removes last element in the dictionary, i.e last key-value pair |
setdefault | key not found inserts the key with default as value. |
update | concatenates 2 dictionary objects |
values | returns values in key-value pair |
>>>d={1:2, 2:3,3:4,4:5,5:6,5:7} >>> >>>d {1: 2, 2: 3, 3: 4, 4: 5, 5: 7}
keys() function returns all keys in the dictionary, as a dict_keys object
>>>d.keys() dict_keys([1, 2, 3, 4, 5])
keys() function returns all values in the dictionary, as a dict_values object
>>>d.values() dict_values([2, 3, 4, 5, 7])
get(key, default=None) function returns value for a given key. Optional default value can be used for key not found.
>>>d.get(5) 7 >>>d.get(10,'key not found') 'key not found' >>>
fromkeys(iterable, value=None function returns Dictionary Object, extracts keys from the iterable, and values set to value
>>d1 = >d.fromkeys(d,10) {1: 10, 2: 10, 3: 10, 4: 10, 5: 10} -- keys from dictionary 'd' value set to 10, orignal dictionary unchanged.
setdefault(key, default=None) function inserts a key with a value in the dictionary,if key not found.
returns key, otheriwse value default
>>>
popitem() function returns a Last Item in the dictionary as a 2 element Tuple --> (key,value)
calling this function on empty dictionary throws Exception .KeyError.
>>> d {1: 2, 2: 3, 3: 4, 4: 5, 5: 7} >>> d.popitem() (5, 7) >>> d {1: 2, 2: 3, 3: 4, 4: 5} >>> d.popitem() (4, 5) >>> d {1: 2, 2: 3, 3: 4} >>>
pop(key, default=None) function removes the specified key,returns the value,if key found.
if key not found, returns default if given otheriwse KeyError exception is thrown
>>>d
{1: 2, 2: 3, 3: 4, 4: 5, 5: 7}
--remove key 5,returns value 7
>>>d.pop(5)
7
>>>d
{1: 2, 2: 3, 3: 4, 4: 5}
>>>d.pop(5,'key not found')
'key not found'
we already deleted key 5, so it is printing default value 'key not found'
>>>d.pop(5)
KeyError: 5 --, key=5 not found in the dictionary, and default value not provided, keyerror exception is thrown
items() function returns dict_items object, dict_items has set where each key:value pair as a tuple as an element
>>>d {1: 2, 2: 3, 3: 4, 4: 5, 5: 7} >>>d.items() dict_items([(1, 2), (2, 3), (3, 4)])
update() function is used to concatenate 2 dictionary objects.
key:value pair from dictionary2 ,added into dictionary 1. dictionary 2 is unchanged
>>> d1={1:2,2:3} >>> d2={3:4,4:5} >>> d1.update(d2) >>> d1 {1: 2, 2: 3, 3: 4, 4: 5} >>> d2 {3: 4, 4: 5}
copy() function is used to shallow-copy from one dictionary object to another.
>>> d={1:2,2:3} >>> d {1: 2, 2: 3} >>> d_copy {1: 2, 2: 3} >>>
clear() function is used to remove all elements from the dictionary.
length of the dictionary is 0.
>>> d={1:2,2:3} >>> >>> d {} >>> len(d) 0
ADS