Python – Convert String List to Key-Value List dictionary
Given a string, convert it to key-value list dictionary, with key as 1st word and rest words as value list.
Input : test_list = [“gfg is best for geeks”, “CS is best subject”] Output : {‘gfg’: [‘is’, ‘best’, ‘for’, ‘geeks’], ‘CS’: [‘is’, ‘best’, ‘subject’]} Explanation : 1st elements are paired with respective rest of words as list. Input : test_list = [“gfg is best for geeks”] Output : {‘gfg’: [‘is’, ‘best’, ‘for’, ‘geeks’]} Explanation : 1st elements are paired with respective rest of words as list.
Method #1 : Using split() + loop
In this, we iterate for each string, and then perform split of string using split(), assign 1st element as key and pack other elements into list, using * operator and create key-value list pair.
Python3
# Python3 code to demonstrate working of# Convert String List to Key-Value List dictionary# Using split() + loop# initializing listtest_list = ["gfg is best for geeks", "I love gfg", "CS is best subject"]# printing stringprint("The original list : " + str(test_list))res = dict()for sub in test_list: # split() for key # packing value list key, *val = sub.split() res[key] = val# printing resultsprint("The key values List dictionary : " + str(res)) |
The original list : ['gfg is best for geeks', 'I love gfg', 'CS is best subject']
The key values List dictionary : {'gfg': ['is', 'best', 'for', 'geeks'], 'I': ['love', 'gfg'], 'CS': ['is', 'best', 'subject']}Time Complexity: O(n2) -> (loop +split)
Space Complexity: O(n)
Method #2 : Using split() + dictionary comprehension
This is similar way to solve this problem. In this, dictionary comprehension is used to solve this problem.
Python3
# Python3 code to demonstrate working of# Convert String List to Key-Value List dictionary# Using split() + dictionary comprehension# initializing listtest_list = ["gfg is best for geeks", "I love gfg", "CS is best subject"]# printing stringprint("The original list : " + str(test_list))# using dictionary comprehension to solve this problemres = {sub[0] : sub[1:] for sub in (ele.split() for ele in test_list)}# printing resultsprint("The key values List dictionary : " + str(res)) |
The original list : ['gfg is best for geeks', 'I love gfg', 'CS is best subject']
The key values List dictionary : {'gfg': ['is', 'best', 'for', 'geeks'], 'I': ['love', 'gfg'], 'CS': ['is', 'best', 'subject']}Time Complexity: O(n2)
Space Complexity: O(n)



Please Login to comment...