Alikula
Alikula

Reputation: 458

Input as List of dictionary python

str_arr = input()
    d = "}, "

    for line in str_arr:
        new_str = [e + '},' for e in str_arr.split(d) if e]

    print(len(new_str))
    for i in new_str:
        print(i)

I want my code to accept a list of dictionaries but the input is a string and tried to make the input str_list to a list but my code adds }, at the end of the console output how can I remove it.enter image description here

Upvotes: 0

Views: 641

Answers (1)

nikeros
nikeros

Reputation: 3379

In the for loop, should you simply do:

new_str = [eval(e.strip()) for e in str_arr.split(",")]

I assume that str_arr is an input like {"A":1},{"B":2,"C":3}, in other words, a string of comma separated dictionaries

Upvotes: 1

Related Questions