Home > database >  Convert list of dictionaries into list of sets?
Convert list of dictionaries into list of sets?

Time:11-13

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.

CodePudding user response:

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.

CodePudding user response:

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.

CodePudding user response:

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)

CodePudding user response:

Since the set does not preserve the order of elements we may not be able to get the exact order of elements.

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'}]

r = [{item for sublist in d.items() for item in sublist} for d in lst]

print(r)

#output

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

Question on set order preservation

Why don't Python sets preserve insertion order?

  • Related