Python program to split the string and convert it to dictionary
Last Updated :
12 Apr, 2023
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*geeks', delim = “*”
Output : {0: 'gfg', 1: 'is', 2: 'best', 3: 'for', 4: 'geeks'}
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
test_str = 'gfg_is_best_for_geeks'
print ( "The original string is : "
+ str (test_str))
delim = "_"
temp = test_str.split(delim)
res = dict ()
for idx, ele in enumerate (temp):
res[idx] = ele
print ( "Dictionary after splits ordering : "
+ str (res))
|
Output:
The original string is : gfg_is_best_for_geeks Dictionary after splits ordering : {0: ‘gfg’, 1: ‘is’, 2: ‘best’, 3: ‘for’, 4: ‘geeks’}
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
test_str = 'gfg_is_best_for_geeks'
print ( "The original string is : "
+ str (test_str))
delim = "_"
res = {idx: ele for idx, ele in
enumerate (test_str.split(delim))}
print ( "Dictionary after splits ordering : "
+ str (res))
|
Output:
The original string is : gfg_is_best_for_geeks
Dictionary after splits ordering : {0: ‘gfg’, 1: ‘is’, 2: ‘best’, 3: ‘for’, 4: ‘geeks’}
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
import re
def Convert(lst):
res_dct = {i: lst[i] for i in range ( 0 , len (lst))}
return res_dct
string = 'gfg_is_best_for_geeks'
print ( "The original string is : "
+ str (string))
regex = '[a-zA-Z]+'
match = re.findall(regex, string)
res = Convert(match)
print ( "Dictionary after splits ordering : "
+ str (res))
|
Output:
The original string is : gfg_is_best_for_geeks
Dictionary after splits ordering : {0: ‘gfg’, 1: ‘is’, 2: ‘best’, 3: ‘for’, 4: ‘geeks’}
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_geeks'
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))
|
Output
The original string is : gfg_is_best_for_geeks
Dictionary after splits ordering : {0: 'gfg', 1: 'is', 2: 'best', 3: 'for', 4: 'geeks'}
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
test_str = 'gfg_is_best_for_geeks'
print ( "The original string is : " + str (test_str))
delim = "_"
res = {}
for i, val in enumerate (test_str.split(delim)):
res.update({i: val})
print ( "Dictionary after splits ordering : " + str (res))
|
Output
The original string is : gfg_is_best_for_geeks
Dictionary after splits ordering : {0: 'gfg', 1: 'is', 2: 'best', 3: 'for', 4: 'geeks'}
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.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...