Built-in functions and dictionary processing operations
Contents
- 1. Built-in method len(). Number of items in the dictionary
- 2. Access to a dictionary item by its key D[key]
- 3. Operation del. Delete item by key
- 4. Operation in. Determining the presence of a key in a dictionary
- 5. Operation not in. Determining the absence of a key in a dictionary
- Related topics
Search other websites:
1. Built-in method len().Number of items in the dictionary
The built-in len() function returns the number of items in a given dictionary. According to Python documentation, the general form of using this function is as follows
count = len(D)
where
- D – source dictionary;
- count – the required number of elements.
Example.
# Function len() - get the number of items in the dictionary # Example 1 Months = { 1:'Jan', 2:'Feb', 3:'Mar', 4:'Apr', 5:'May', 6:'Jun', 7:'Jul', 8:'Aug', 9:'Sep', 10:'Oct', 11:'Nov', 12:'Dec' } count = len(Months) # count = 12 # Example 2. Using nested dictionaries Position = { 'Manager': { 'Director', 'Deputy Director' }, 'Teacher': { 'Specialist', 'Methodist', 'Senior Lecturer' }, 'Staff': { 'Cleaner', 'Porter', 'Watchman' } } count1 = len(Position) # count1 = 3 count2 = len(Position['Manager']) # count2 = 2 count3 = len(Position['Staff']) # count3 = 3
⇑
2. Access to a dictionary item by its key D[key]
Using the [] operation, you can access the value if the key by which this value is written is known. The access operation [] allows you to both read and write values in the dictionary.
According to the Python documentation, the general form of using the [] operation to read a value is as follows:
item = D[key]
here
- D – specified dictionary;
- key – the key on the basis of which the item is searched in the dictionary. If the key key is not in the dictionary, then a KeyError exception is thrown;
- item – the item that is obtained by the key key.
If you need to write or add the item value to the dictionary using the key key, then the operation [] has the following form of use
D[key] = item
There are two possible cases during recording:
- key is present in the dictionary. In this case, the value of item replaces the previous value;
- key is not in the dictionary. In this case, a new key:item pair is added to the dictionary.
Example.
# Operation [] - access by the key to dictionary items # Example 1 # Source dictionary WorkDays = { 1:'Mon', 2:'Tue' } # Get an item item = WorkDays[2] # item = 'Tue' # Instead of 'Tue' write 'Sat' WorkDays[2] = 'Sat' # WorkDays = {1: 'Mon', 2: 'Sat'} # Add new pair (6:'Sun') WorkDays[6] = 'Sun' # WorkDays = {1: 'Mon', 2: 'Sat', 6: 'Sun'} # Example 2. Using nested dictionaries SubCat1 = { 1:'A', 2:'B', 3:'C' } SubCat2 = { 4:'D', 5:'E' } Category = { 'High':SubCat1, 'Low' :SubCat2 } # Get an item item = Category['Low'] # item = {4: 'D', 5: 'E'} item2 = Category['Low'][4] # item2 = 'D' # Add item to dictionary Category['Middle'] = { 6:'J', 7:'К' } # Change the item in dictionary Category['High'][3] = 'F' # Category = {'High': {1:'A', 2:'B', 3:'F'}, # 'Low': {4:'D', 5:'E'}, # 'Middle': {6:'J', 7:'K'}}
⇑
3. Operation del. Delete item by key
The del operation is intended to remove an element from the dictionary based on the given key key. The general form of using the operation is as follows
del D[key]
where
- D – given dictionary;
- key – The key in the dictionary whose item you want to delete.
If you specify a key that does not exist, a KeyError exception will be thrown.
Example.
# Operation del - delete item from dictionary # Source dictionary Salary = { 'Director': 120800.0, 'Secretary': 101150.25, 'Locksmith': 188200.00 } # Delete item by key 'Secretary' del Salary['Secretary'] # Salary = {'Director': 120800.0, # 'Locksmith': 188200.0} # Attempting to delete a key that does not exist # del Salary[5] - this is impossible, a KeyError exception is thrown: 5 # del Salary['None'] - also forbidden
⇑
4. Operation in. Determining the presence of a key in a dictionary
To determine if a given key exists in a dictionary, Python uses the in operation. The general form of using the in operation is as follows
f_is = key in D
where
- D – source dictionary;
- key – the key whose presence in the dictionary D needs to be determined;
- f_is – the result of a logical type. If f_is = True, then the key key is present in the dictionary. If f_is = False, then the key is not in the dictionary.
Example. The example uses the in operation to determine if there is a Salary key in the dictionary that needs to be deleted. The operation is used in the conditional if statement.
# Operation in - determining the presence of a key in a dictionary # Source dictionary Salary = { 'Director': 120800.0, 'Secretary': 101150.25, 'Locksmith': 188200.00 } # Delete item by key 'Secretary' with check key = 'Secretary' if key in Salary: del Salary['Secretary'] # Salary = {'Director': 120800.0, # 'Locksmith': 188200.0} # Attempting to delete a key that does not exist # if there is no key, then a KeyError exception is not thrown key2 = 5 if key2 in Salary: del Salary[key2]
⇑
5. Operation not in. Determining the absence of a key in a dictionary
The not in operation returns always the opposite of the in operation. The general form of the not in operation is as follows:
f_is = key not in D
where
- D – source dictionary;
- key – the key whose presence in the dictionary D needs to be determined;
- f_is – result of logical type. If f_is = True, then the key key is not in the dictionary D. If f_is = False, then the key key is present in the dictionary D.
Example. The example demonstrates the use of the not in operation to determine if the key that was entered from the keyboard is present in the dictionary.
# Operation not in - determination of the absence # of a key in the dictionary # Formation of a dictionary of words with their numerical equivalent # 1. Create an empty dictionary Words = dict() # Words = {} # 2. Enter the number of words in the dictionary count = int(input("Number of words: ")) # 3. The cycle of adding words i=0 while i<count: print("Input word:") wrd = str(input("Text:")) value = int(input("Value: ")) # If the wrd key is not in the dictionary, then add the [wrd:value] pair if wrd not in Words: Words[wrd] = value i=i+1 # Display the generated dictionary print("\nYou enter the following information:") print(Words)
The result of the program:
Number of words: 3 Input word: Text:wrd1 Value: 100 Input word: Text:wrd2 Value: 200 Input word: Text:wrd3 Value: 250 You enter the following information: {'wrd1': 100, 'wrd2': 200, 'wrd3': 250}
⇑
Related topics
- Dictionaries. Basic concepts. Specifications. Creating dictionaries. Accessing dictionary values
- Methods of working with dictionaries
- Working with dictionaries. Build-in functions list(), zip(). Dictionary bypass. Dictionary generators. Sorting. Combinations of dictionaries with sets
⇑