The Wayback Machine - https://web.archive.org/web/20230321012344/https://www.geeksforgeeks.org/python-construct-dictionary-key-value-pairs-separated-by-delimiter/
Skip to content
Related Articles
Open in App
Not now

Related Articles

Python – Construct dictionary Key-Value pairs separated by delimiter

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

Given a String with key-value pairs separated by delim, construct a dictionary.

Input : test_str = ‘gfg#3, is#9, best#10’, delim = ‘#’ 
Output : {‘gfg’: ‘3’, ‘is’: ‘9’, ‘best’: ’10’} 
Explanation : gfg paired with 3, as separated with # delim.

Input : test_str = ‘gfg.10’, delim = ‘.’ 
Output : {‘gfg’: ’10’} 
Explanation : gfg paired with 10, as separated with . delim. 

Method #1 : Using split() + loop

In this, we perform a split on comma, to get key value pairs, and again a split on custom delim to get key value pairs separated. Then assigned to dictionary using loop.

Python3




# Python3 code to demonstrate working of
# Construct dictionary Key-Value pairs separated by delim
# Using split() + loop
 
# initializing strings
test_str = 'gfg$3, is$9, best$10'
 
# printing original string
print("The original string is : " + str(test_str))
 
# initializing delim
delim = "$"
 
# split by comma for getting different dict values
dicts = test_str.split(', ')
 
res = dict()
for sub in dicts:
 
    # 2nd split for forming Key-Values for dictionary
    res[sub.split(delim)[0]]  = sub.split(delim)[1]
 
# printing result
print("The constructed dictionary : " + str(res))

Output

The original string is : gfg$3, is$9, best$10
The constructed dictionary : {'gfg': '3', 'is': '9', 'best': '10'}

Time complexity: O(n).
Auxiliary space: O(n).

Method #2 : Using dictionary comprehension + split()

Similar to above method, just the difference being that dictionary comprehension is used for performing task of dictionary construction.

Python3




# Python3 code to demonstrate working of
# Construct dictionary Key-Value pairs separated by delim
# Using split() + dictionary comprehension
 
# initializing strings
test_str = 'gfg$3, is$9, best$10'
 
# printing original string
print("The original string is : " + str(test_str))
 
# initializing delim
delim = "$"
 
# split by comma for getting different dict values
dicts = test_str.split(', ')
 
# dictionary comprehension to form dictionary
res = {sub.split(delim)[0] : sub.split(delim)[1] for sub in dicts}
 
# printing result
print("The constructed dictionary : " + str(res))

Output

The original string is : gfg$3, is$9, best$10
The constructed dictionary : {'gfg': '3', 'is': '9', 'best': '10'}

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

Method 3: Using Regular Expressions

Regular expressions can be used to extract the key-value pairs from the string using a pattern that matches the delimiters and the keys and values. 

Python3




import re
 
test_str = 'gfg$3, is$9, best$10'
delim = "\$|, "  # pattern for matching delimiter
 
pattern = f"([^\{delim}]+){delim}([^\{delim}]+)"
matches = re.findall(pattern, test_str)
 
res = {key: value for key, value in matches}
 
print("The constructed dictionary : " + str(res))

Output

The constructed dictionary : {'gfg': '', '': 'best'}

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

Method #4: Using str.partition()

Split the string into substrings based on the first occurrence of the delimiter and use str.partition() method to extract the key-value pairs.

Python3




test_str = 'gfg$3, is$9, best$10'
 
res = {}
 
for pair in test_str.split(', '):
    key, _, value = pair.partition('$')
    res[key] = int(value)
 
print("The constructed dictionary : " + str(res))

Output

The constructed dictionary : {'gfg': 3, 'is': 9, 'best': 10}

Time complexity: O(n), where n is the length of the input string.
Auxiliary space: O(n), because we are creating a dictionary with n/2 key-value pairs, and each key and value is a string of length at most n.


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!