Learn Python - Python tutorial - python dictionary update - Python examples - Python programs
1. fromkeys(seq,value) :- This method is used to declare a new dictionary from the sequence mentioned in its arguments. This function can also initialize the declared dictionary with “value” argument.
2. update(dic) :- This function is used to update the dictionary to add other dictionary keys.
python - Sample - python code :
python programming - Output :
The updated dictionary is : {'Age': 19, 'Name': 'Nandini', 'ID': 2541997} The new dictionary values are : {'Age': 5, 'Name': 5, 'ID': 5}
3. has_key() :- This function returns true if specified key is present in the dictionary, else returns false.
4. get(key, def_val) :- This function return the key value associated with the key mentioned in arguments. If key is not present, the default value is returned.
python - Sample - python code :
python programming - Output :
Name is a key The value associated with ID is : Not Present The dictionary values are : {'Name': 'Nandini', 'Age': 19}
5. setdefault(key, def_value) :- This function also searches for a key and displays its value like get() but, it creates new key with def_value if key is not present.
python - Sample - python code :
python programming - Output :
The value associated with Age is : No ID The dictionary values are : {'Name': 'Nandini', 'Age': 19, 'ID': 'No ID'}