Skip to content
Related Articles
Open in App
Not now

Related Articles

Python – Test for Incrementing Dictionary

Improve Article
Save Article
  • Last Updated : 24 Mar, 2023
Improve Article
Save Article

Given a dictionary, test if it is incrementing, i.e its key and values are increasing by 1.

Input : test_dict = {1:2, 3:4, 5:6, 7:8} 
Output : True 
Explanation : All keys and values in order differ by 1.

Input : test_dict = {1:2, 3:10, 5:6, 7:8} 
Output : False 
Explanation : Irregular items. 

Method 1: Using items() + loop + extend() + list comprehension

In this, 1st step is to get the dictionary to list conversion using items() + list comprehension and extend(), next loop is used to test if the converted list is incremental.

Python3




# Python3 code to demonstrate working of
# Test for Incrementing Dictionary
# Using extend() + list comprehension
 
# initializing dictionary
test_dict = {1: 2, 3: 4, 5: 6, 7: 8}
 
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
 
temp = []
 
# forming list from dictionary
[temp.extend([key, val]) for key, val in test_dict.items()]
 
# checking for incrementing elements
res = True
for idx in range(0, len(temp) - 1):
 
    # test for increasing list
    if temp[idx + 1] - 1 != temp[idx]:
        res = False
 
# printing result
print("Is dictionary incrementing : " + str(res))


Output

The original dictionary is : {1: 2, 3: 4, 5: 6, 7: 8}
Is dictionary incrementing : True

Time complexity: O(n), where n is the number of items in the dictionary. The time complexity is determined by the for loop that iterates through the list formed from the dictionary.
Auxiliary space: O(n), where n is the number of items in the dictionary. The auxiliary space is determined by the use of a list “temp” that stores the key-value pairs from the dictionary.

Method 2: Using keys(),values() and sort() method

Python3




# Python3 code to demonstrate working of
# Test for Incrementing Dictionary
 
# initializing dictionary
test_dict = {1: 2, 3: 10, 5: 6, 7: 8}
 
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
res = False
x = list(test_dict.keys())
y = list(test_dict.values())
a = []
for i in range(0, len(x)):
    a.append(x[i])
    a.append(y[i])
b = []
b.extend(a)
b.sort()
if(a == b):
    res = True
 
# printing result
print("Is dictionary incrementing : " + str(res))


Output

The original dictionary is : {1: 2, 3: 10, 5: 6, 7: 8}
Is dictionary incrementing : False

Time Complexity: O(n log n)
Auxiliary Space: O(n)

Method 3: Using replace(),list(),map(),extend(),sort() methods

Python3




# Python3 code to demonstrate working of
# Test for Incrementing Dictionary
 
# initializing dictionary
test_dict = {1: 2, 3: 10, 5: 6, 7: 8}
 
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
res = False
x = str(test_dict)
x = x.replace("{", "")
x = x.replace("}", "")
x = x.replace(":", "")
x = x.replace(",", "")
y = x.split()
y = list(map(int, y))
a = []
a.extend(y)
y.sort()
if(a == y):
    res = True
# printing result
print("Is dictionary incrementing : " + str(res))


Output

The original dictionary is : {1: 2, 3: 10, 5: 6, 7: 8}
Is dictionary incrementing : False

Time complexity: O(n log n), where n is the number of key-value pairs in the dictionary.
Auxiliary space: O(n), where n is the number of key-value pairs in the dictionary.

Method 4: Using all() and zip()

Python3




def is_incrementing(dictionary):
    # Use the built-in `all` function to check if all elements in the generator expression are `True`
    # The generator expression `(val - 1 == prev for prev, val in zip(dictionary.keys(), dictionary.values()))`
    # generates a sequence of booleans that represent whether the difference between each key and value is 1
    # If all elements in the sequence are `True`, `all` returns `True`; otherwise, it returns `False`
    return all(val - 1 == prev for prev, val in zip(dictionary.keys(), dictionary.values()))
 
 
# Define the test dictionary
test_dict = {1: 2, 3: 9, 5: 6, 7: 8}
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
# Call the function and pass in the test dictionary as an argument
print("Is dictionary incrementing:", is_incrementing(test_dict))
# This code is contributed by Jyothi pinjala


Output

The original dictionary is : {1: 2, 3: 9, 5: 6, 7: 8}
Is dictionary incrementing: False

Time Complexity: O(n)
Auxiliary Space: O(1)


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!