how can I check the value for a condition? They are in the list of dictionaries and the dictionary is in the list

Question:

How can this be done or through which library is easier ?
I get a list of dictionaries and I want to take the data from the "Client" and overwrite it so that in the future I can write to xlsx without any problems.
And it would probably be more correct to recreate the list of dictionaries

list_1 = [{
      "Object": "info",
      "subobject": "info",
      "Name": "info",
      "Ip": "info",
      "Status": "info",
      "Mac": "info",
      "Worker": "info",
      "Sn": "info",
      "Client": [
         "Name Max #4F6128"
         "1 CH #F79646"
      ]
   },
   {
      "Object": "info",
      "subobject": "info",
      "Name": "info",
      "Ip": "info",
      "Status": "info",
      "Mac": "info"
      "Worker": "info",
      "Sn": "info",
      "Client": [
         "Name Alex #4F6128"
         "3 CH #F79646"
         "loi #92D050"
      ]
   }]

as a result, I need to somehow get the data from the client and categorize it. You can still name by colors: #F79646,#4F6128,#92D050, #A00000" and it should turn out like this:

result = [{
          "Object": "info",
          "subobject": "info",
          "Name": "info",
          "Ip": "info",
          "Status": "info",
          "Mac": "info",
          "Worker": "info",
          "Sn": "info",
          "#4F6128": "Name Max"
          "#F79646": "1 CH"
          "#92D050": "no"
          "#A00000":"no"
             
          ]
       },
       {
          "Object": "info",
          "subobject": "info",
          "Name": "info",
          "Ip": "info",
          "Status": "info",
          "Mac": "info"
          "Worker": "info",
          "Sn": "info",
          "#4F6128": "Name Alex"
          "#F79646": "3 CH"
          "#92D050": "loi"
          "#A00000":"no"

       }]

I tried using if, but I don’t understand how to write the condition for checking correctly

Asked By: Alexey

||

Answers:

Code:

result = []

for d in list_1:
    check_for = ['#F79646', '#4F6128', '#92D050', '#A00000']
    new_d = {}
    for k, v in d.items():
        if k != 'Client':
            new_d[k] = v
        else:
            for x in v:
                if x.split()[-1] in check_for:
                    check_for.remove(x.split()[-1])
                    new_d[x.split()[-1]] = ' '.join(x.split()[:-1])
            for y in check_for:
                new_d[y] = 'no'
    result.append(new_d)

print(result)

Output:

[{'#4F6128': 'Name Max',
  '#92D050': 'no',
  '#A00000': 'no',
  '#F79646': '1 CH',
  'Ip': 'info',
  'Mac': 'info',
  'Name': 'info',
  'Object': 'info',
  'Sn': 'info',
  'Status': 'info',
  'Worker': 'info',
  'subobject': 'info'},
 {'#4F6128': 'Name Alex',
  '#92D050': 'loi',
  '#A00000': 'no',
  '#F79646': '3 CH',
  'Ip': 'info',
  'Mac': 'info',
  'Name': 'info',
  'Object': 'info',
  'Sn': 'info',
  'Status': 'info',
  'Worker': 'info',
  'subobject': 'info'}]
Answered By: The Thonnu
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.