How to grouping list by value and put in new dynamic variable using Python

Question:

How to grouping list by value and put in new dynamic variable using Python.
I have some data list like this and i want to store in new dynamic variable list based on value K93, K94, K0JA => (this will be a dynamic value)

data_list = [
(('K93', 'AMK2FJVGB', 0.0, 600.0, 300), [300, 15]),
(('K93', 'AMK2FJSRM', 0.0, 600.0, 300), [300, 15]),
(('K93', 'AMK2FJVGB', 0.0, 600.0, 300), [300, 15]),
(('K94', 'AMK2FJSRM', 0.0, 600.0, 300), [300, 15]),
(('K93', 'AMK2FJVGB', 0.0, 600.0, 300), [300, 15]),
(('K93', 'AMK2FJSRM', 0.0, 600.0, 300), [300, 15]),
(('K0JA', 'AMK0JA  ', 0.0, 700.0, 400), [600, 30]),
(('K0JA', 'AMK0JA  ', 0.0, 700.0, 400), [300, 15]),
(('K93', 'AMK2FJNBK', 0.0, 800.0, 400), [300.0, 15]),
(('K94', 'AMK2FJNBK', 0.0, 800.0, 400), [300.0, 15])]

i want result like this:

list_K93 = [
(('K93', 'AMK2FJVGB', 0.0, 600.0, 300), [300, 15]),
(('K93', 'AMK2FJSRM', 0.0, 600.0, 300), [300, 15]),
(('K93', 'AMK2FJVGB', 0.0, 600.0, 300), [300, 15]),
(('K93', 'AMK2FJVGB', 0.0, 600.0, 300), [300, 15]),
(('K93', 'AMK2FJSRM', 0.0, 600.0, 300), [300, 15]),
(('K93', 'AMK2FJNBK', 0.0, 800.0, 400), [300.0, 15]),
]

list_K94 = [
(('K94', 'AMK2FJSRM', 0.0, 600.0, 300), [300, 15]),
(('K94', 'AMK2FJNBK', 0.0, 800.0, 400), [300.0, 15])
]

list_K0JA = [
(('K0JA', 'AMK0JA  ', 0.0, 700.0, 400), [600, 30]),
(('K0JA', 'AMK0JA  ', 0.0, 700.0, 400), [300, 15])
]
Asked By: Fikri Sambasri

||

Answers:

You can cluster data via a dictionary like this:

result = {}
for i in data_list:
    if i[0][0] in result:
        result[i[0][0]].append(i)
    else:
        result[i[0][0]] = []
        result[i[0][0]].append(i)

So that the result would be a dictionary like this:

{'K93': [(('K93', 'AMK2FJVGB', 0.0, 600.0, 300), [300, 15]),
         (('K93', 'AMK2FJSRM', 0.0, 600.0, 300), [300, 15]),
         (('K93', 'AMK2FJVGB', 0.0, 600.0, 300), [300, 15]),
         (('K93', 'AMK2FJVGB', 0.0, 600.0, 300), [300, 15]),
         (('K93', 'AMK2FJSRM', 0.0, 600.0, 300), [300, 15]),
         (('K93', 'AMK2FJNBK', 0.0, 800.0, 400), [300.0, 15])],
 'K94': [(('K94', 'AMK2FJSRM', 0.0, 600.0, 300), [300, 15]),
         (('K94', 'AMK2FJNBK', 0.0, 800.0, 400), [300.0, 15])],
 'K0JA': [(('K0JA', 'AMK0JA  ', 0.0, 700.0, 400), [600, 30]),
         (('K0JA', 'AMK0JA  ', 0.0, 700.0, 400), [300, 15])]}
Answered By: Parsa

Instead of dynamically named variables, you should create a dict with the desired keys instead, which can be done by initializing each new key of the output dict with a list and appending each tuple entry to the list:

data_dict = {}
for t in data_list:
    data_dict.setdefault(t[0][0], []).append(t)

data_dict becomes:

{
     'K93': [(('K93', 'AMK2FJVGB', 0.0, 600.0, 300), [300, 15]),
             (('K93', 'AMK2FJSRM', 0.0, 600.0, 300), [300, 15]),
             (('K93', 'AMK2FJVGB', 0.0, 600.0, 300), [300, 15]),
             (('K93', 'AMK2FJVGB', 0.0, 600.0, 300), [300, 15]),
             (('K93', 'AMK2FJSRM', 0.0, 600.0, 300), [300, 15]),
             (('K93', 'AMK2FJNBK', 0.0, 800.0, 400), [300.0, 15])],
     'K94': [(('K94', 'AMK2FJSRM', 0.0, 600.0, 300), [300, 15]),
             (('K94', 'AMK2FJNBK', 0.0, 800.0, 400), [300.0, 15])],
     'K0JA': [(('K0JA', 'AMK0JA  ', 0.0, 700.0, 400), [600, 30]),
              (('K0JA', 'AMK0JA  ', 0.0, 700.0, 400), [300, 15])]
}
Answered By: blhsing

You can create dynamic variable like this

i = "test"
globals()[f"dynamic_variable_{i}"] = i
print(dynamic_variable_test)

Here is the code

 data_list = [
   (('K93', 'AMK2FJVGB', 0.0, 600.0, 300), [300, 15]),
   (('K93', 'AMK2FJSRM', 0.0, 600.0, 300), [300, 15]),]
   
def create_dynamic_var(name,value):
  globals()[f"list_{name}"] = []
  globals()[f"list_{name}"].append(value)

  
for elem in data_list:
     print(elem[0][0])
     create_dynamic_var(elem[0][0],elem)
     
print(list_K93)
Answered By: Kiran S