update() in python dictionary

Previous
Next

Update(): A pre-defined method is used to update the dictionary

keys = [101, 102, 103, 104]
values = ["harin","kiritin","ramya","annie"]

accounts = dict() #create an empty dictionary
for i in range(len(keys)):
    key = keys[i]
    value = values[i]
    accounts.update({key:value})
    
print("Account details are :")
for accno in accounts:
    name = accounts.get(accno)
    print("%d --> %s" %(accno, name))

Output:

Account details are:
101 --> harin
102 --> kiritin
103 --> ramya
104 --> annie

Built-in Functions with Dictionary

all() : Return True if all keys of the dictionary are true (or if the dictionary is empty).

any() : Return True if any key of the dictionary is true. If the dictionary is empty, return False.

len() : Return the length (the number of items) in the dictionary.

sorted() : Return a new sorted list of keys in the dictionary.

len() : It returns the number of items (length) of an object.

Previous
Next

Add Comment

Courses Enquiry Form