.feed-links {display:none !important;} -->

Python dictionaries

 dictionary in python is like a list, but instead of using indexes , we use alphanumeric keys.

How to create a python dictionary?

To initialize a dictionary , we use the following syntax:

>>>  a  =  {}

or

>>>  a  =  dict ()

How to add values ​​in a python dictionary?

To add values ​​to a dictionary you must indicate a key as well as a value:

>>>  a  =  {} 
>>>  a [ "name" ]  =  "Wayne" 
>>>  a [ "first name" ]  =  "Bruce" 
>>>  a 
{ 'last name' :  'Wayne' ,  'first name' :  'Bruce' }

You can use numeric keys as in list logic.

How to get a value in a python dictionary?

The get method allows you to retrieve a value from a dictionary and if the key is not found, you can give a value to return by default:

>>>  data  =  { "name" :  "Wayne" ,  "age" :  45 } 
>>>  data . get ( "name" ) 
'Wayne' 
>>>  data . get ( "address" ,  "Unknown address" ) 
'Unknown address'

How to check for the presence of a key in a python dictionary?

You can use the haskey method to check for the presence of a key you are looking for:

>>>  a . has_key ( "name" ) 
True

How to delete an entry in a python dictionary?

It is possible to delete an entry by indicating its key, as for the lists:

>>>  del  a [ "name" ] 
>>>  a 
{ 'first name' :  'Bruce' }

How to get the keys of a python dictionary by a loop?

To retrieve the keys we use the keys method .

>>>  file  =  { "name" : "Wayne" ,  "first name" : "Bruce" } 
>>>  for  cle  in  file . keys (): 
...      print  key 
...  
name 
first name

How to get the values ​​of a python dictionary by a loop?

For this we use the values method .

>>>  file  =  { "name" : "Wayne" ,  "first name" : "Bruce" } 
>>>  for  value  in  file . values (): 
...      print  value 
...  
Wayne 
Bruce

How to get the keys and values ​​of a python dictionary by a loop?

To retrieve the keys and the values ​​at the same time, we use the items method which returns a tuple .

>>>  file  =  { "name" : "Wayne" , "first name" : "Bruce" } 
>>>  for  key , value  in  file . items (): 
...      print  key ,  value 
...  
name  Wayne 
firstname  Bruce

How to use tuples as a key in a python dictionary?

One of the strengths of python is the tuple / dictionary combination which works wonders in some cases such as when using coordinates.

>>>  b  =  {} 
>>>  b [( 32 )] = 12 
>>>  b [( 45 )] = 13 
>>>  b 
{( 4 ,  5 ):  13 ,  ( 3 ,  2 ):  12 }

How to create an independent copy of a python dictionary?

As with any variable, you cannot copy a dictionary by doing dic1 = dic2 :

>>>  d  =  { "k1" : "Bruce" ,  "k2" : "Wayne" } 
>>>  e  =  d 
>>>  d [ "k1" ]  =  "XXX" 
>>>  e 
{ 'k2' :  'Wayne' ,  'k1' :  'XXX' }

To create an independent copy you can use the copy method :

>>>  d  =  { "k1" : "Bruce" ,  "k2" : "Wayne" } 
>>>  e  =  d . copy () 
>>>  d [ "k1" ]  =  "XXX" 
>>>  e 
{ 'k2' :  'Wayne' ,  'k1' :  'Bruce' }

How to merge python dictionaries?

The update method allows you to merge two dictionaries .

>>>  a  =  { 'name' :  'Wayne' } 
>>>  b  =  { 'first name' :  'bruce' } 
>>>  a . update ( b ) 
>>>  print ( a ) 
{ 'name' :  'Wayne' ,  'firstname' :  'Bruce' }   

No comments:

Post a Comment