Convert key-value pair comma separated string into dictionary - Python Last Updated : 22 Jan, 2025 Summarize Comments Improve Suggest changes Share Like Article Like Report In Python, we might have a string containing key-value pairs separated by commas, where the key and value are separated by a colon (e.g., "a:1,b:2,c:3"). The task is to convert this string into a dictionary where each key-value pair is represented properly. Let's explore different ways to achieve this.Using Dictionary Comprehension with splitThis method uses dictionary comprehension to split the string and create a dictionary. Python # Input string s = "a:1,b:2,c:3" # Converting to dictionary using dictionary comprehension d = {k: int(v) for k, v in (item.split(":") for item in s.split(","))} print(d) Output{'a': 1, 'b': 2, 'c': 3} Explanation:The string is split by commas to get individual key-value pairs.Each pair is further split by a colon to separate the key and value.Dictionary comprehension creates a dictionary with the key and integer-converted value.Let's explore some more ways and see how we can convert key-value pair comma separated string into dictionary.Table of ContentUsing map() with splitUsing a For LoopUsing Regular ExpressionsUsing map() with splitThis method uses map() and split to process the string and convert it into a dictionary. Python # Input string s = "a:1,b:2,c:3" # Converting to dictionary using map d = dict(map(lambda x: (x.split(":")[0], int(x.split(":")[1])), s.split(","))) print(d) Output{'a': 1, 'b': 2, 'c': 3} Explanation:The string is split by commas into a list of key-value pairs.map() applies a lambda function to split each pair by a colon and convert the value to an integer.The dict() function creates a dictionary from the mapped pairs.Using For LoopThis method uses a simple for loop to build the dictionary. Python # Input string s = "a:1,b:2,c:3" # Converting to dictionary using a loop d = {} for item in s.split(","): k, v = item.split(":") d[k] = int(v) print(d) Output{'a': 1, 'b': 2, 'c': 3} Explanation:The string is split by commas to get the key-value pairs.Each pair is split by a colon to separate the key and value.The key-value pair is added to the dictionary after converting the value to an integer.Using Regular ExpressionsThis method uses the re module to extract key-value pairs from the string. Python import re # Input string s = "a:1,b:2,c:3" # Extracting key-value pairs using regex matches = re.findall(r'(\w+):(\d+)', s) d = {k: int(v) for k, v in matches} print(d) Output{'a': 1, 'b': 2, 'c': 3} Explanation:A regular expression matches key-value pairs in the string.re.findall() returns a list of tuples containing the keys and values.A dictionary comprehension converts the tuples into a dictionary, with the values cast to integers. Comment More infoAdvertise with us Next Article Python - Construct dictionary Key-Value pairs separated by delimiter G garg_ak0109 Follow Improve Article Tags : Python Python Programs Python dictionary-programs Python string-programs Practice Tags : python Similar Reads Convert byte string Key:Value Pair of Dictionary to String - Python In Python, dictionary keys and values are often stored as byte strings when working with binary data or certain encoding formats, we may need to convert the byte strings into regular strings. For example, given a dictionary {b'key1': b'value1', b'key2': b'value2'}, we might want to convert it to {'k 3 min read Python - Convert key-value String to dictionary Sometimes, while working with Python strings, we can have problems in which we need to convert a string's key-value pairs to the dictionary. This can have applications in which we are working with string data that needs to be converted. Let's discuss certain ways in which this task can be performed. 4 min read 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': 7 min read Add a key value pair to Dictionary in Python The task of adding a key-value pair to a dictionary in Python involves inserting new pairs or updating existing ones. This operation allows us to expand the dictionary by adding new entries or modify the value of an existing key.For example, starting with dictionary d = {'key1': 'geeks', 'key2': 'fo 3 min read Python - Convert key-values list to flat dictionary We are given a list that contains tuples with the pairs of key and values we need to convert that list into a flat dictionary. For example a = [("name", "Ak"), ("age", 25), ("city", "NYC")] is a list we need to convert it to dictionary so that output should be a flat dictionary {'name': 'Ak', 'age': 3 min read Convert List Of Dictionary into String - Python In Python, lists can contain multiple dictionaries, each holding key-value pairs. Sometimes, we need to convert a list of dictionaries into a single string. For example, given a list of dictionaries [{âaâ: 1, âbâ: 2}, {âcâ: 3, âdâ: 4}], we may want to convert it into a string that combines the conte 3 min read Like