#

#

sampleqa.in tutorials, python tutorial

Python tutorial


Python Dictionary Methods

     Dictionary is a mapped data structure, which has key:value pair as an elements, key is immutable object, values can be mutable

methoddescription
clearremoves all elements
copydoes shallow-copy
fromkeyscreates a new dictionary object, with keys from iterable object, value for each key is default
getgets value for a given key
itemsreturns set, where each element in the set is a tuple,each tuple has key and value pair
keysreturns keys
popremoves element for a given key
popitemremoves last element in the dictionary, i.e last key-value pair
setdefaultkey not found inserts the key with default as value.
updateconcatenates 2 dictionary objects
valuesreturns values in key-value pair

Create a Dictionary Object

	
	>>>d={1:2, 2:3,3:4,4:5,5:6,5:7}
	>>>
	>>>d
	{1: 2, 2: 3, 3: 4, 4: 5, 5: 7}
	

dictionary keys() function

     keys() function returns all keys in the dictionary, as a dict_keys object

		>>>d.keys()
		
		dict_keys([1, 2, 3, 4, 5])

	

dictionary values() function

     keys() function returns all values in the dictionary, as a dict_values object

		>>>d.values()
		
		dict_values([2, 3, 4, 5, 7])

	

dictionary get() function

     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'
		>>>
	

dictionary fromkeys() function

     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.
	

dictionary setdefault() function

     setdefault(key, default=None) function inserts a key with a value in the dictionary,if key not found.
returns key, otheriwse value default

		>>>
	

dictionary popitem() function

     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}
	>>> 
		
	

dictionary pop() function

     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
	

dictionary items() function

     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)])

	

dictionary update() function

     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}


	

dictionary copy() function

     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}
		>>> 
	

dictionary clear() function

     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