Python – Construct dictionary Key-Value pairs separated by delimiter
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 stringstest_str = 'gfg$3, is$9, best$10'# printing original stringprint("The original string is : " + str(test_str))# initializing delimdelim = "$"# split by comma for getting different dict valuesdicts = 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 resultprint("The constructed dictionary : " + str(res)) |
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 stringstest_str = 'gfg$3, is$9, best$10'# printing original stringprint("The original string is : " + str(test_str))# initializing delimdelim = "$"# split by comma for getting different dict valuesdicts = test_str.split(', ')# dictionary comprehension to form dictionaryres = {sub.split(delim)[0] : sub.split(delim)[1] for sub in dicts}# printing resultprint("The constructed dictionary : " + str(res)) |
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 retest_str = 'gfg$3, is$9, best$10'delim = "\$|, " # pattern for matching delimiterpattern = f"([^\{delim}]+){delim}([^\{delim}]+)"matches = re.findall(pattern, test_str)res = {key: value for key, value in matches}print("The constructed dictionary : " + str(res)) |
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)) |
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.


Please Login to comment...