Diamondhands
Diamondhands

Reputation: 41

Convert list of dictionaries into list of sets?

I have dictionaries within a list like this:

[{'market': 'singapore', 'abbreviation': 'sg', 'indexId': 'STI', 'indexName': 'STRAITS TIMES INDEX'}, {'market': 'thailand', 'abbreviation': 'th', 'indexId': 'SET100', 'indexName': 'SET100 INDEX'}, {'market': 'turkey', 'abbreviation': 'tr', 'indexId': 'XUTEK', 'indexName': 'BIST TEKNOLOJI'}, {'market': 'thailand', 'abbreviation': 'th', 'indexId': 'SET50', 'indexName': 'SET50 INDEX'}]

The desired results should look like this

[{'market', 'singapore', 'abbreviation', 'sg', 'indexId', 'STI', 'indexName', 'STRAITS TIMES INDEX'}, {'market', 'thailand', 'abbreviation', 'th', 'indexId', 'SET100', 'indexName', 'SET100 INDEX'}, {'market', 'turkey', 'abbreviation', 'tr', 'indexId', 'XUTEK', 'indexName', 'BIST TEKNOLOJI'}, {'market', 'thailand', 'abbreviation': 'th', 'indexId', 'SET50', 'indexName', 'SET50 INDEX'}]

How can I possibly remove the ":" within this list of dictionaries? I know I can use the re.sub() function, but I don't know how to apply it in this scenario.

Upvotes: 1

Views: 78

Answers (3)

S.B
S.B

Reputation: 16476

Not a good idea. You might be able to do it using re.sub() (by converting your data to its string representation) but lets say "nobody does that". Regex is powerful tool to deal with other problems. Here better not using it.

You can do the following:

res = [{*d.keys(), *d.values()} for d in lst]
print(res)

Basically you unpack both .keys() and .values() iterators into a set. Final result is a set.

Upvotes: 2

Michael M.
Michael M.

Reputation: 11070

Those characters are not part of a string, they are formatting provided by the print() function. It seems that you want to convert a list of dictionaries into a list of lists. You can do that like this with a list comprehension:

lst = [{'market': 'singapore', 'abbreviation': 'sg', 'indexId': 'STI', 'indexName': 'STRAITS TIMES INDEX'}, {'market': 'thailand', 'abbreviation': 'th', 'indexId': 'SET100', 'indexName': 'SET100 INDEX'}, {'market': 'turkey', 'abbreviation': 'tr', 'indexId': 'XUTEK', 'indexName': 'BIST TEKNOLOJI'}, {'market': 'thailand', 'abbreviation': 'th', 'indexId': 'SET50', 'indexName': 'SET50 INDEX'}]

res = [[item for sublist in dct.items() for item in sublist] for dct in lst]
print(res)

Alternatively, you can use this for a list of sets:

lst = [{'market': 'singapore', 'abbreviation': 'sg', 'indexId': 'STI', 'indexName': 'STRAITS TIMES INDEX'}, {'market': 'thailand', 'abbreviation': 'th', 'indexId': 'SET100', 'indexName': 'SET100 INDEX'}, {'market': 'turkey', 'abbreviation': 'tr', 'indexId': 'XUTEK', 'indexName': 'BIST TEKNOLOJI'}, {'market': 'thailand', 'abbreviation': 'th', 'indexId': 'SET50', 'indexName': 'SET50 INDEX'}]

res = [set([item for sublist in dct.items() for item in sublist]) for dct in lst]
print(res)

Upvotes: 0

Amir reza Riahi
Amir reza Riahi

Reputation: 2450

That's not a set in the first form. That's a dictionary. To put all dict's keys and values into a set you can do it like this:

>>> [set(list(i.keys()) + list(i.values())) for i in x]
[{'singapore', 'abbreviation', 'sg', 'STI', 'STRAITS TIMES INDEX', 'indexId', 'indexName', 'market'}, {'th', 'abbreviation', 'SET100 INDEX', 'thailand', 'indexId', 'SET100', 'indexName', 'market'}, {'BIST TEKNOLOJI', 'market', 'abbreviation', 'indexId', 'tr', 'XUTEK', 'indexName', 'turkey'}, {'th', 'SET50 INDEX', 'abbreviation', 'thailand', 'indexId', 'indexName', 'SET50', 'market'}]

Where x is your list.

Upvotes: 0

Related Questions