Home > Back-end >  transform the list into a dictionary with items
transform the list into a dictionary with items

Time:06-03

List1 = ["1-Delphinia-65.61.138.207", 
"2-Lennie-74.37.240.220", 
"3-Shayne-174.76.131.156", 
"4-Veriee-59.20.115.49", 
"5-Val-172.101.94.229", 
"6-Lanny-1.16.47.151", 
"7-Laureen-117.2.197.11", 
"8-Kristyn-63.21.195.62", 
"9-Arlana-92.84.0.112", 
"10-Arney-49.145.219.15"]

Transform the list into a dictionary with items in the following format

{
    “Delphine” : {
        “id” : 1,
        “Ipaddr”: 65.61.138.207
    },
    “Lennie” : { ……. }, 
…….
}

CodePudding user response:

Use dictionary comperhension:

>>> {name: {'id': int(id_), 'Ipaddr': ipaddr} for id_, name, ipaddr in map(lambda s: s.split('-'), List1)}
{'Delphinia': {'id': 1, 'Ipaddr': '65.61.138.207'},
 'Lennie': {'id': 2, 'Ipaddr': '74.37.240.220'},
 'Shayne': {'id': 3, 'Ipaddr': '174.76.131.156'},
 'Veriee': {'id': 4, 'Ipaddr': '59.20.115.49'},
 'Val': {'id': 5, 'Ipaddr': '172.101.94.229'},
 'Lanny': {'id': 6, 'Ipaddr': '1.16.47.151'},
 'Laureen': {'id': 7, 'Ipaddr': '117.2.197.11'},
 'Kristyn': {'id': 8, 'Ipaddr': '63.21.195.62'},
 'Arlana': {'id': 9, 'Ipaddr': '92.84.0.112'},
 'Arney': {'id': 10, 'Ipaddr': '49.145.219.15'}}

CodePudding user response:

You can simply use this code:

    List1 = ["1-Delphinia-65.61.138.207", "2-Lennie-74.37.240.220", "3-Shayne-174.76.131.156", "4-Veriee-59.20.115.49", "5-Val-172.101.94.229", "6-Lanny-1.16.47.151", "7-Laureen-117.2.197.11", "8-Kristyn-63.21.195.62", "9-Arlana-92.84.0.112", "10-Arney-49.145.219.15"]


final_dict = {}
for item in List1:
    data = item.split('-')

    final_dict[data[1]] = {'id':int(data[0]), 'Ipaddr': data[2]}

print(final_dict)

CodePudding user response:

Other approach, using a dictionary comprehension:

{name: {'id': ID, 'Ipaddr': ip} for s in List1 for ID, name, ip in [s.split('-')]}

output:

{'Delphinia': {'id': '1', 'Ipaddr': '65.61.138.207'},
 'Lennie': {'id': '2', 'Ipaddr': '74.37.240.220'},
 'Shayne': {'id': '3', 'Ipaddr': '174.76.131.156'},
 'Veriee': {'id': '4', 'Ipaddr': '59.20.115.49'},
 'Val': {'id': '5', 'Ipaddr': '172.101.94.229'},
 'Lanny': {'id': '6', 'Ipaddr': '1.16.47.151'},
 'Laureen': {'id': '7', 'Ipaddr': '117.2.197.11'},
 'Kristyn': {'id': '8', 'Ipaddr': '63.21.195.62'},
 'Arlana': {'id': '9', 'Ipaddr': '92.84.0.112'},
 'Arney': {'id': '10', 'Ipaddr': '49.145.219.15'}}

CodePudding user response:

This is how I'd do it with my basic python knowledge

new_dict= {}
for item in List1:
  newitem=item.split("-")
  new_dict[newitem[1]]={'id':int(newitem[0]),'ip':newitem[2]}
print(new_dict)

Output is

{
  "Delphinia":{
    "id":1,
    "ip":"65.61.138.207"
  },
  "Lennie":{
    "id":2,
    "ip":"74.37.240.220"
  },
  "Shayne":{
    "id":3,
    "ip":"174.76.131.156"
  },
  "Veriee":{
    "id":4,
    "ip":"59.20.115.49"
  },
  "Val":{
    "id":5,
    "ip":"172.101.94.229"
  },
  "Lanny":{
    "id":6,
    "ip":"1.16.47.151"
  },
  "Laureen":{
    "id":7,
    "ip":"117.2.197.11"
  },
  "Kristyn":{
    "id":8,
    "ip":"63.21.195.62"
  },
  "Arlana":{
    "id":9,
    "ip":"92.84.0.112"
  },
  "Arney":{
    "id":10,
    "ip":"49.145.219.15"
  }

CodePudding user response:

 def Convert(lst):
    res_dct = {lst[i 1]: {"id":lst[i],"Ipaddr":lst[i   2]} for i in range(0,len(lst), 3)}
    return res_dct

 final_dict ={}
 for i in list1:
     l =(i.split('-'))
     new = Convert(l)
     final_dict.update(new)

output >> final_dict
{'Delphinia': {'id': '1', 'Ipaddr': '65.61.138.207'},
 'Lennie': {'id': '2', 'Ipaddr': '74.37.240.220'},
 'Shayne': {'id': '3', 'Ipaddr': '174.76.131.156'},
 'Veriee': {'id': '4', 'Ipaddr': '59.20.115.49'},
 'Val': {'id': '5', 'Ipaddr': '172.101.94.229'},
 'Lanny': {'id': '6', 'Ipaddr': '1.16.47.151'},
 'Laureen': {'id': '7', 'Ipaddr': '117.2.197.11'},
 'Kristyn': {'id': '8', 'Ipaddr': '63.21.195.62'},
 'Arlana': {'id': '9', 'Ipaddr': '92.84.0.112'},
 'Arney': {'id': '10', 'Ipaddr': '49.145.219.15'}}
  • Related