Python program to split the string and convert it to dictionary
Given a delimiter (denoted as delim in code) separated string, order the splits in form of dictionary.
Examples:
Input : test_str = 'gfg*is*best*for*Beginner', delim = “*” Output : {0: 'gfg', 1: 'is', 2: 'best', 3: 'for', 4: 'Beginner'}
Input : test_str = 'gfg*is*best', delim = “*” Output : {0: 'gfg', 1: 'is', 2: 'best'}
Method 1 : Using split() + loop
This is a brute way in which this task can be performed. In this, the split sections are stored in a temporary list using split() and then the new dictionary is created from the temporary list.
Python3
# Using split() + loop # initializing string test_str = 'gfg_is_best_for_Beginner' # printing original string print ( "The original string is : " + str (test_str)) # initializing delim delim = "_" # splitting using split() temp = test_str.split(delim) res = dict () # using loop to reform dictionary with splits for idx, ele in enumerate (temp): res[idx] = ele # printing result print ( "Dictionary after splits ordering : " + str (res)) |
Output:
The original string is : gfg_is_best_for_Beginner Dictionary after splits ordering : {0: ‘gfg’, 1: ‘is’, 2: ‘best’, 3: ‘for’, 4: ‘Beginner’}
Method 2 : Using dictionary comprehension + split() + enumerate()
This is a shorthand method with the help of which this task can be performed. In this, we perform the task of dictionary reconstruction using one-liner dictionary(dictionary comprehension) and enumerate() is used to help in ordering.
Python3
# Using dictionary comprehension + split() + enumerate() # initializing string test_str = 'gfg_is_best_for_Beginner' # printing original string print ( "The original string is : " + str (test_str)) # initializing delim delim = "_" # using one liner to rearrange dictionary res = {idx: ele for idx, ele in enumerate (test_str.split(delim))} # printing result print ( "Dictionary after splits ordering : " + str (res)) |
Output:
The original string is : gfg_is_best_for_Beginner
Dictionary after splits ordering : {0: ‘gfg’, 1: ‘is’, 2: ‘best’, 3: ‘for’, 4: ‘Beginner’}
Method 3 : Using re.findall() + dict comprehension
This is a brute way in which this task can be performed. In this, we find all the words from string using re.findall and form dictionary with find array of words.
Python
# A Python re.findall import re #Function to convert the array to object def Convert(lst): res_dct = {i: lst[i] for i in range ( 0 , len (lst))} return res_dct # A sample text string string = 'gfg_is_best_for_Beginner' # printing original string print ( "The original string is : " + str (string)) # A sample regular expression to find digits. regex = '[a-zA-Z]+' match = re.findall(regex, string) res = Convert(match) # printing result print ( "Dictionary after splits ordering : " + str (res)) |
Output:
The original string is : gfg_is_best_for_Beginner
Dictionary after splits ordering : {0: ‘gfg’, 1: ‘is’, 2: ‘best’, 3: ‘for’, 4: ‘Beginner’}
Method 4: Using dict.fromkeys() and zip() functions:
Step-by-step algorithm:
- Initialize the input string “test_str”.
- Print the original string.
- Initialize the delimiter “delim” and split the input string using split() and delimiter “delim” and store the result in “temp”.
- Initialize a dictionary “res” with keys as range(len(temp)).
- Use the zip() function to combine the keys and values of “res” with the elements of “temp”.
- Update the dictionary “res” with the combined key-value pairs.
- Print the result.
Python3
test_str = 'gfg_is_best_for_Beginner' print ( "The original string is : " + str (test_str)) delim = "_" temp = test_str.split(delim) res = dict .fromkeys( range ( len (temp))) for key, val in zip (res.keys(), temp): res[key] = val print ( "Dictionary after splits ordering : " + str (res)) |
The original string is : gfg_is_best_for_Beginner Dictionary after splits ordering : {0: 'gfg', 1: 'is', 2: 'best', 3: 'for', 4: 'Beginner'}
Time Complexity: O(N), where N is the length of the input string.
Auxiliary Space: O(N), where N is the length of the input string.
Method #5:Using for loop and dictionary.update() method:
- Initialize the input string
- Print the original string
- Initialize the delimiter
- Initialize an empty dictionary
- Iterate over the list obtained by splitting the string using the delimiter using for loop.
- For each element in the list, add a key-value pair to the dictionary using the update() method
- Print the resulting dictionary
Python3
# initializing string test_str = 'gfg_is_best_for_Beginner' # printing original string print ( "The original string is : " + str (test_str)) # initializing delim delim = "_" # initialize an empty dictionary res = {} # iterate over the list obtained by splitting the string using the delimiter for i, val in enumerate (test_str.split(delim)): # add key-value pair to the dictionary using update() method res.update({i: val}) # printing result print ( "Dictionary after splits ordering : " + str (res)) |
The original string is : gfg_is_best_for_Beginner Dictionary after splits ordering : {0: 'gfg', 1: 'is', 2: 'best', 3: 'for', 4: 'Beginner'}
Time Complexity: O(N), where N is the length of the input string.
Auxiliary Space: O(N), where N is the length of the input string.
Contact Us