cancel
Showing results forย 
Search instead forย 
Did you mean:ย 
Data Engineering
Join discussions on data engineering best practices, architectures, and optimization strategies within the Databricks Community. Exchange insights and solutions with fellow data engineers.
cancel
Showing results forย 
Search instead forย 
Did you mean:ย 

How to add dictionary {key: value} using literal_eval in python?

Kaniz_Fatma
Community Manager
Community Manager
 
1 ACCEPTED SOLUTION

Accepted Solutions

Hubert-Dudek
Esteemed Contributor III

Using literal_eval is not needed (and it is not good idea - when you want to evaluate something answer is don't do it). Just use something from below code:

our_dict[key] = value
 
# for example in list comprehension
our_dict = {value: count for count, value in enumerate(['a', 'b', 'c', 'd'])}
print(our_dict)
# > {'a': 0, 'b': 1, 'c': 2, 'd': 3}

View solution in original post

1 REPLY 1

Hubert-Dudek
Esteemed Contributor III

Using literal_eval is not needed (and it is not good idea - when you want to evaluate something answer is don't do it). Just use something from below code:

our_dict[key] = value
 
# for example in list comprehension
our_dict = {value: count for count, value in enumerate(['a', 'b', 'c', 'd'])}
print(our_dict)
# > {'a': 0, 'b': 1, 'c': 2, 'd': 3}

Connect with Databricks Users in Your Area

Join a Regional User Group to connect with local Databricks users. Events will be happening in your city, and you wonโ€™t want to miss the chance to attend and share knowledge.

If there isnโ€™t a group near you, start one and help create a community that brings people together.

Request a New Group