-1

I have this list here:

mechs= [
        {
            'type_code': 'GAT',
            'name': 'STRIKE',
            'unit_price': 3500,
            'minimum_kilo': 500,
        },
        {
            'type_code': 'GAT',
            'name': 'BLITZ',
            'unit_price': 2500,
            'minimum_kilo': 520,
        },
        {
            'type_code': 'ZGMF',
            'name': 'LEGEND',
            'unit_price': 4500,
            'minimum_kilo': 550,
        },
    ]

Then I wanted the output to be like:

mechas=
{'GAT': {
[
{'type_code': 'GAT',
  'name': 'STRIKE',
  'unit_price': 3500,
  'minimum_kilo': 500},
{'type_code': 'GAT',
  'name': 'BLITZ',
  'unit_price': 2500,
  'minimum_kilo': 520}
]
}

#continue to next dict...

How do I get the 'GAT' dict to put the next value(dict) to the dict instead of reassigning them?

Here is what I have used:

mechas={}
for i in mechs:
    mechas[i['type_code']]=i

And the output is like this:

{'GAT': {'type_code': 'GAT',
  'name': 'BLITZ',
  'unit_price': 2500,
  'minimum_kilo': 520},
 'ZGMF': {'type_code': 'ZGMF',
  'name': 'LEGEND',
  'unit_price': 4500,
  'minimum_kilo': 550}}

This output does not include the previous value (code GAT name STRIKE), so how do I keep it instead of reassigning a new value?

I actually tried list(), append(), etc. but I think it's too long to put it here and the result still does not satisfy me.

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
  • The output you want isn't valid, you can't put a list in a set. Do you want a dictionary whose values are _lists_? – jonrsharpe Mar 20 '22 at 09:15
  • @jonrsharpe ah i see, yes i want a dictionary that values are set, and can contain thoose mechs values with same code but different names. – dgrszombie315 Mar 20 '22 at 09:17
  • You can't put dictionaries in a set either. The values must be hashable and that generally means immutable. – jonrsharpe Mar 20 '22 at 09:20
  • @jonrsharpe does that mean I can only put 1 dict on the key? do I need to create a new different key to assign the unlisted dict? – dgrszombie315 Mar 20 '22 at 09:34
  • No, it just means you can't use a set, because the values you want to store are dictionaries, which are mutable. There are other containers (a list, as I've already suggested, for example). – jonrsharpe Mar 20 '22 at 09:35
  • okay, so i need to filter by those type_code that have the same that have a code, append the dict to a list for each code, and put it into dict, is that true? – dgrszombie315 Mar 20 '22 at 09:51
  • Yes, sort of. And before you append to the list, first check if the list exists for that key, or use a [`defaultdict`](https://docs.python.org/3/library/collections.html#collections.defaultdict). If you **show your code** in a [**minimal reproducible example**](https://stackoverflow.com/help/minimal-reproducible-example), then you might get answers for how to solve this. – aneroid Mar 20 '22 at 10:05
  • @dgrszombie315 , You wont be able to add data type in a set (_Which is immutable meaning values cant be changed_) which are mutable for example list, dictionary. You can add tuple or sets inside of sets as they are immutable. – Faraaz Kurawle Mar 21 '22 at 09:46
  • @dgrszombie315 , i have posted an answer which i think would work for you. – Faraaz Kurawle Mar 21 '22 at 09:47

2 Answers2

1
a={}
for i in mechs:
    type_code=i.get('type_code')
    if type_code not in a.keys():
        a[type_code]=i
    else:
        c=[a.get(type_code),i]
        a[type_code]=c

How does the above code works?, well here's the explanation:

  • Just checked if a key of type_code is available in the dict or not.
  • If its not available, then created a new key type_code and its value as the index we were reading.
  • If its available, then took all the values to the corresponding type_code and added them into list, and also added the index value to the list. Then changed the previous value of type_code with the newly created list.
Faraaz Kurawle
  • 1,085
  • 6
  • 24
  • Thank You, this worked perfectly, didn't come in my mind i could use keys() and get() – dgrszombie315 Mar 22 '22 at 02:11
  • @dgrszombie315 , glad it helped you. You can press the tick button beside my answer also. – Faraaz Kurawle Mar 22 '22 at 10:50
  • anyway, i tried this but, i created the GAT type code have 3 code, and ZGMF 3 too, i run again the code and it created a list inside a list, followed by a dictionary, i tried to configure around the i and a.get(type_code) but still didnt work. i also tried this https://stackoverflow.com/questions/11492656/creating-a-list-of-dictionaries-results-in-a-list-of-copies-of-the-same-dictiona But i cant still figure it out to create just a list with a dictionaries only – dgrszombie315 May 06 '22 at 11:03
0

That should work

mechas={}
for i in mechs:
  if i['type_code'] not in mechas:
    mechas[i['type_code']]=[]
  mechas[i['type_code']].append(i)
necaris15
  • 11
  • 2