So inside the dictionary, I have values that are lists with [] brackets. But I want to change them into {} brackets. Does anyone have any idea how to do this?
From:
{hello: ['1', '2', '3']}
To:
{hello: {'1', '2', '3'}}
You can convert the list inside the dictionary into a set like you would with any other list, too:
>>> a = {'hello': ['1', '2', '3']}
>>> a
{'hello': ['1', '2', '3']}
>>> a['hello'] = set(a['hello'])
>>> a
{'hello': {'2', '3', '1'}}
Sets aren't ordered, which explains the mixed-up order of {'2', '3', '1'}
.
Also, you didn't define your dictionary key hello
in your question so I used a simple string here.
Assuming "hello" is a variable:
d = {hello: ['1', '2', '3']}
dn = { k: set(v) for k, v in d.items() } #iterate through the dict and replace the lists by sets
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.