2

After creating a list from a couple of text files I get the following :

['host : myhost.ro', 'port : 12222', 'service_name : SRVNM', 'user : MGA', 'password : Test']

Is there a way to make 2 lists like this :

lst1 =['host', 'port', 'service_name', 'user', 'password']
lst2 =['myhost.ro', '12222', 'SRVNM', 'MGA', 'Test']

Or to make a dictionary from the original list, this is my end goal, a dict like this :

Dict['host'] = 'myhost.ro'
MGA
  • 195
  • 1
  • 1
  • 8

6 Answers6

2

You can use a dict comprehension:

lst = ['host : myhost.ro', 'port : 12222', 'service_name : SRVNM', 'user : MGA', 'password : Test']

dct = {key: value for item in lst for key, value in [item.split(" : ")]}
print(dct)

Which yields

{'host': 'myhost.ro', 'port': '12222', 'service_name': 'SRVNM', 'user': 'MGA', 'password': 'Test'}
Jan
  • 42,290
  • 8
  • 54
  • 79
0

You could do this with some combination of str.strip, str.split, map, zip, comprehension generators:

# ...to obtain the `dict`
dict(map(str.strip, x.split(':')) for x in ll)
# {'host': 'myhost.ro', 'port': '12222', 'service_name': 'SRVNM', 'user': 'MGA', 'password': 'Test'}
# ...to obtain the "parallel" sequences (`tuples`)
l1, l2 = zip(*(map(str.strip, x.split(':')) for x in ll))
l1
# ('host', 'port', 'service_name', 'user', 'password')
l2
# ('myhost.ro', '12222', 'SRVNM', 'MGA', 'Test')

Assuming the input to be:

ll = ['host : myhost.ro', 'port : 12222', 'service_name : SRVNM', 'user : MGA', 'password : Test']

Also, it looks like the dict() seems a better option in this use case.

norok2
  • 25,683
  • 4
  • 73
  • 99
  • I don't yet got the 2 lists, I only got the first one, I'm yet to split the first list into 2 lists like that, i wish it was that simple – MGA Apr 14 '20 at 15:10
  • @MGA it is not that difficult either – norok2 Apr 14 '20 at 15:19
0

try this:

data = ['host : myhost.ro', 'port : 12222', 'service_name : SRVNM', 'user : MGA', 'password : Test']

data_dict = dict([tuple(e.split(" : ")) for e in data])
print(data_dict)
Gabio
  • 9,126
  • 3
  • 12
  • 32
  • 2
    Note that this is creating unnecessary temporary objects. A more memory efficient approach would be to drop the `list` comprehension and the `tuple` conversion: `dict(e.split(" : ") for e in data)` – norok2 Apr 14 '20 at 16:26
0
original = ['host : myhost.ro', 'port : 12222', 'service_name : SRVNM', 'user : MGA', 'password : Test']

b = dict([a.split(' : ') for a in original])
Eric M
  • 1,360
  • 11
  • 19
0

You can split your list into two lists that you want like this:

myList = ['host : myhost.ro', 'port : 12222', 'service_name : SRVNM', 'user : MGA', 'password : Test']
list1 = []
list2 = []

for i in myList:
    element1 = i.split(":")[0]
    element2 = i.split(":")[1]
    list1.append(element1)
    list2.append(element2)
print(list1)
print(list2)

Output:

['host ', 'port ', 'service_name ', 'user ', 'password ']                                                             
[' myhost.ro', ' 12222', ' SRVNM', ' MGA', ' Test'] 

Or Your Final desired Dictionary will be achieved by:

newDict = {}

for i in myList:
    key = i.split(":")[0].strip()
    value = i.split(":")[1].strip()
    newDict[key] = value
print(newDict)

Output:

{'user': 'MGA', 'port': '12222', 'service_name': 'SRVNM', 'password': 'Test', 'host': 'myhost.ro'}
Sheri
  • 1,383
  • 3
  • 10
  • 26
-1

Take advantage of string method split and list \ dict comprehensions.

To get your dict:

data = ['host : myhost.ro', 'port : 12222', 'service_name : SRVNM', 'user : MGA', 'password : Test']
{entry.split(' : ')[0]:entry.split(' : ')[1] for entry in data}