Powered by Ajaxy
Digg StumbleUpon LinkedIn YouTube Flickr Facebook Twitter RSS Reset

Python: Check if key exists in dictionary

params = {'Font': 12, 'Style': 'Bold', 'Color': [0, 0, 255]}
print 'style' in params           #False

Python: How to delete a dictionary entry

params = {'Font': 12, 'Style': 'Bold', 'Color': [255, 0, 255]}
print params
# delete style
del params['Style']
print params

Python: Create a new dictionary object

# build a dictionary from a liste and a tuple
dict1= dict([('font', 12), ('size', 12)])
print dict
# build a dictionary from an expression
dict2= d

Python: iterate through a dictionary key list

even  =   [2, 4, 6, 8]
odd   =   [1, 3, 5, 7, 9]
dict={'even':even, 'odd':odd}
for key in dict.keys():
     dict[key] *= 10
print dict.values()
[/p

Python: change dictionary value

con_str = {"host":"127.0.0.1", "database":"mysql", "user":"root", "pwd":"mysql"}
con_str['host']='localhost'
print con_str

Python: Removing a Random Key-Value Pair

# Define a dictionary
params = {'Font': 12, 'Style': 'bold', 'Color': [255, 0, 255]}
# print the dictionary
print params
# delete an arbitrary key-

Python: Clearing or Deleting a Dictionary

# Define a dictionary
con_str = {"host":"127.0.0.1",
           "database":"mysql",
           "user":"root",
           "pwd":"mysql"}
# clear the