You are on page 1of 5

7/27/22, 9:41 AM Python | Convert string dictionary to dictionary - GeeksforGeeks

Data Structures Algorithms Interview Preparation Topic-wise Practice C++ Java Python

Python | Convert string dictionary to


dictionary
Difficulty Level :
Medium ● Last Updated :
22 May, 2019

Interconversions of data types have been discussed many times and have been quite

a popular problem to solve. This ar ticle discusses yet another problem of

interconversion of dictionar y, in string format to a dictionar y. Let ’s discuss cer tain

ways in which this can be done.

Method #1 : Using json.loads()

This task can easily be per formed using the inbuilt function of loads of json librar y of

python which conver ts the string of valid dictionar y into json form, dictionar y in

P ython.

# Python3 code to demonstrate


# convert dictionary string to dictionary
# using json.loads()
import json
  
# initializing string 
test_string = '{"Nikhil" : 1, "Akshat" : 2, "Akash" : 3}' 
  
# printing original string 
print("The original string : " + str(test_string))
  
# using json.loads()
# convert dictionary string to dictionary
res = json.loads(test_string)
  
# print result
print("The converted dictionary : " + str(res))

We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge
Output : that you have read and understood our
Cookie Policy &
Privacy Policy

Got It !

https://www.geeksforgeeks.org/python-convert-string-dictionary-to-dictionary/?ref=lbp 1/5
7/27/22, 9:41 AM Python | Convert string dictionary to dictionary - GeeksforGeeks

Start Your Coding Journey Now!


The original string : {"Nikhil" : 1, "Akshat"
Login : 2, "Akash" : 3}

Register
The converted dictionary : {'Nikhil': 1, 'Akshat': 2, 'Akash': 3}

Method #2 : Using ast.literal_eval()

The above method can also be used to per form a similar conversion. Function safer

than the eval function and can be used for interconversion of all data types other than

dictionar y as well.

# Python3 code to demonstrate


# convert dictionary string to dictionary
# using ast.literal_eval()
import ast
  
# initializing string 
test_string = '{"Nikhil" : 1, "Akshat" : 2, "Akash" : 3}' 
  
# printing original string 
print("The original string : " + str(test_string))
  
# using ast.literal_eval()
# convert dictionary string to dictionary
res = ast.literal_eval(test_string)
  
# print result
print("The converted dictionary : " + str(res))

Output :

The original string : {"Nikhil" : 1, "Akshat" : 2, "Akash" : 3}

The converted dictionary : {'Nikhil': 1, 'Akshat': 2, 'Akash': 3}

We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge
that you have read and understoodLike
our
Cookie
11
Policy &
Privacy Policy

Got It !

https://www.geeksforgeeks.org/python-convert-string-dictionary-to-dictionary/?ref=lbp 2/5
7/27/22, 9:41 AM Python | Convert string dictionary to dictionary - GeeksforGeeks

Start Your Coding Journey Now!


Previous
Login Register
Next

RECOMMENDED ARTICLES Page : 1 2 3

Python | Convert flattened Python | Convert byteString


01 05
dictionary into nested dictionary key:value pair of dictionary to
01, Apr 19
String
11, Feb 19

Python | Convert nested dictionary Python | Convert key-value pair


02
into flattened dictionary 06
comma separated string into
02, Apr 19
dictionary
03, Jun 19

Python - Convert Dictionary Value


03
list to Dictionary List Python - Convert Dictionary to
07
01, Jul 20
Concatenated String
21, Apr 20

Python | Convert dictionary object Python - Convert key-value String


04 08
into string to dictionary
21, Jan 19 21, Apr 20

Ar ticle Contributed By :

manjeet_04
@manjeet_04

Vote for difficulty


We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge
that you have read and understood our
Cookie Policy &
Privacy Policy
Current difficulty :
Medium

Got It !
Easy Normal Medium Hard Expert
https://www.geeksforgeeks.org/python-convert-string-dictionary-to-dictionary/?ref=lbp 3/5
7/27/22, 9:41 AM Python | Convert string dictionary to dictionary - GeeksforGeeks

Start Your Coding Journey Now! Login Register

Article Tags : Python dictionary-programs, Python, Python Programs

Improve Article Report Issue

Writing code in comment?


Please use ide.geeksforgeeks.org,
generate link and share the link here.

Load Comments

A-143, 9th Floor, Sovereign Corporate Tower,

Sector-136, Noida, Uttar Pradesh - 201305


feedback@geeksforgeeks.org

Company Learn
About Us Algorithms
Careers Data Structures
In Media SDE Cheat Sheet
Contact Us Machine learning
Privacy Policy CS Subjects
Copyright Policy Video Tutorials
Courses

News Languages
Python
TopyouNews
We use cookies to ensure have the best browsing experience on our website. By using our site, you
acknowledge
Java
that you have read and understood our
Cookie Policy &
Privacy Policy
Technology
Got It ! CPP
Work & Career
https://www.geeksforgeeks.org/python-convert-string-dictionary-to-dictionary/?ref=lbp 4/5
7/27/22, 9:41 AM Python | Convert string dictionary to dictionary - GeeksforGeeks

Business Golang
Start Your Coding
Finance Journey Now!
Login
C#
Register

Lifestyle SQL
Knowledge Kotlin

Web Development Contribute


Web Tutorials Write an Article
Django Tutorial Improve an Article
HTML Pick Topics to Write
JavaScript Write Interview Experience
Bootstrap Internships
ReactJS Video Internship
NodeJS

@geeksforgeeks
, Some rights reserved

We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge
that you have read and understood our
Cookie Policy &
Privacy Policy

Got It !

https://www.geeksforgeeks.org/python-convert-string-dictionary-to-dictionary/?ref=lbp 5/5

You might also like