-4

I have a list like this :

['Wi-Fi', '10.0.10.22', '255.255.0.0', '34-F3-9A-4C-D0-C4', 'VMware Network Adapter VMnet1', '192.168.10.111', '255.255.255.0', '00-50-56-C0-00-01', 'Loopback Pseudo-Interface 1', '127.0.0.1', '255.0.0.0', '00-00-00-00-00-00-00-E0']

How can i convert it to dictionary like below :

{'1' : ['Wi-Fi', '10.0.10.22', '255.255.0.0', '34-F3-9A-4C-D0-C4'], '2' : ['VMware Network Adapter VMnet1', '192.168.10.111', '255.255.255.0', '00-50-56-C0-00-01'], '3' : ['Loopback Pseudo-Interface 1', '127.0.0.1', '255.0.0.0', '00-00-00-00-00-00-00-E0']}

Many thanks, Quang

Park Yo Jin
  • 331
  • 1
  • 3
  • 15

4 Answers4

1

If you want to split by 4 use this function for instance: How do you split a list into evenly sized chunks?

l = ['Wi-Fi', '10.0.10.22', '255.255.0.0', '34-F3-9A-4C-D0-C4', 'VMware Network Adapter VMnet1', '192.168.10.111', '255.255.255.0', '00-50-56-C0-00-01', 'Loopback Pseudo-Interface 1', '127.0.0.1', '255.0.0.0', '00-00-00-00-00-00-00-E0']

def chunks(l, n):
    """Yield successive n-sized chunks from l."""
    for i in range(0, len(l), n):
        yield l[i:i + n]

d = {ind:i for ind, i in enumerate(chunks(l, 4), 1)}

d returns:

{1: ['Wi-Fi', '10.0.10.22', '255.255.0.0', '34-F3-9A-4C-D0-C4'],
 2: ['VMware Network Adapter VMnet1',
  '192.168.10.111',
  '255.255.255.0',
  '00-50-56-C0-00-01'],
 3: ['Loopback Pseudo-Interface 1',
  '127.0.0.1',
  '255.0.0.0',
  '00-00-00-00-00-00-00-E0']}
Anton vBR
  • 18,287
  • 5
  • 40
  • 46
1

Assuming that your rule is to split the list every four items, you can do it like so:

res = {i+1: n[i*4:4*(i+1)] for i in range(len(n)//4)}

which gives:

{1: ['Wi-Fi', '10.0.10.22', '255.255.0.0', '34-F3-9A-4C-D0-C4'], 
 2: ['VMware Network Adapter VMnet1', '192.168.10.111', '255.255.255.0', '00-50-56-C0-00-01'], 
 3: ['Loopback Pseudo-Interface 1', '127.0.0.1', '255.0.0.0', '00-00-00-00-00-00-00-E0']}

Or if you are allergic to slicing:

n_i = iter(n)
res = {i+1: [next(n_i) for _ in range(4)] for i in range(len(n)//4)}
Ma0
  • 15,057
  • 4
  • 35
  • 65
1

Inspired by the grouper recipe in itertools documentation:

l = ['Wi-Fi', '10.0.10.22', '255.255.0.0', '34-F3-9A-4C-D0-C4', 
     'VMware Network Adapter VMnet1', '192.168.10.111', '255.255.255.0', '00-50-56-C0-00-01', 
     'Loopback Pseudo-Interface 1', '127.0.0.1', '255.0.0.0', '00-00-00-00-00-00-00-E0']

d = {i: sublist for i, sublist in enumerate(zip(*([iter(l)]*4)))}

print(d)

#{0: ('Wi-Fi', '10.0.10.22', '255.255.0.0', '34-F3-9A-4C-D0-C4'), 
# 1: ('VMware Network Adapter VMnet1', '192.168.10.111', '255.255.255.0', '00-50-56-C0-00-01'), 
# 2: ('Loopback Pseudo-Interface 1', '127.0.0.1', '255.0.0.0', '00-00-00-00-00-00-00-E0')}
Thierry Lathuille
  • 23,663
  • 10
  • 44
  • 50
1
input = ['Wi-Fi', '10.0.10.22', '255.255.0.0', '34-F3-9A-4C-D0-C4', 'VMware Network Adapter VMnet1', '192.168.10.111', '255.255.255.0', '00-50-56-C0-00-01', 'Loopback Pseudo-Interface 1', '127.0.0.1', '255.0.0.0', '00-00-00-00-00-00-00-E0']

d = {}
for i in range(1,len(input)/3):
    d[i] = []

item = iter(input)
for i in range(len(d)):
    d[i+1] = [next(item) for x in range(len(d)+1)]
print d

The result as d would be {1: ['Wi-Fi', '10.0.10.22', '255.255.0.0', '34-F3-9A-4C-D0-C4'], 2: ['VMware Network Adapter VMnet1', '192.168.10.111', '255.255.255.0', '00-50-56-C0-00-01'], 3: ['Loopback Pseudo-Interface 1', '127.0.0.1', '255.0.0.0', '00-00-00-00-00-00-00-E0']}

lola
  • 115
  • 2
  • 6