1

trying to figure out how to do this and have yet to find a good solution. I pulled this data out of an XML response. It was in a var tag. Now what I would like to do is create a dictionary out of it. The domain.com should be paired with the number right listed behind it.

This is the data:

[
    'cb131.domain1.com', '147827', 
    'cb143.domain2.com', '147825', 
    'cb175.domain1.com', '147454', 
    'cb190.domain.com', '146210', 
    'cb201.domain.com', '146208', 
    'cb219.domain.com', '146042', 
    'cb225.domain.com', '146282', 
    'cb900.domain.com', '148461', 
    'cb901.domain.com', '148493', 
    'cb902.domain.com', '148495', 
    'cb903.domain.com', '148497', 
    'cb904.domain.com','148499', 
    'cb905.domain.com', '148501', 
    'cb906.domain.com', '148503', 
    'cb907.domain.com', '148505', 
    'cb908.domain.com', '148507', 
    'cb909.domain.com', '148509'
]

So for example cb131.domain1.com should be paired with 147827, cb143.domain2.com paired with 147825 and so on.

Drawing a blank on a good quick solution on how to do this. Hopefully someone can help.

Thanks!

Edited with answer I choose below:

I choose this answer and also to help anyone else I add a nice way to print out the results (data is the string I obtained):

import ast
i = iter(ast.literal_eval(data))
dic = dict(zip(i, i))
for key , value in dic.items():
    print(key, " :: ", value)
  • Actually I looked at those answers and none of them are as clean as the answer I was given on my question. Also they are similar questions and not exact. Each one has a different starting point and wanting a different result that doesn't match mine. So I respectively disagree with you marking it as a duplicate. – John Murray Feb 08 '19 at 11:47

4 Answers4

1

This should do it. Assuming the list is saved to a variable l:

keys = l[::2]
vals = l[1::2]
dic  = dict(zip(keys, vals))
devneal17
  • 281
  • 1
  • 4
  • 14
  • When i try this and print the dic I get this: {'[': "'", 'c': 'o', '1': '4', 'd': 'c', '-': 'o', 'm': ' ', 'l': 'o', 'u': 'd', 'e': 'x', 'r': 'g', "'": ']', ' ': "'", '7': '.', '2': '.', '3': '.', '5': '.', '0': '9', '6': '.','9': '.', '4': '.', '8': '5'} – John Murray Feb 07 '19 at 22:53
0

I do not yet know Python that I can write a snippet, but:

  • initialize an empty dictionary in Python
  • create a for loop counting index from 0 to length of your array in steps of two.
    • inside add a dictionary entry with key of value at index and value at index + 1
    • perhaps check for duplicates

Does this answer help you?

This is Python - quickly google'd:

dictionary = { }
for idx in range(0, len(data), 2)
   dictionary[data[idx]] = data[idx + 1]
BitLauncher
  • 587
  • 4
  • 15
0

Assuming you have the above in a python array called data, you can do:

new_data = []
for i in range(0, len(data), 2):
    new_data.append((data[i], data[i+1]))

Now new_data would be a list of tuples. You could certainly create a better data structure to hold these pairs if you want.

ajon
  • 7,868
  • 11
  • 48
  • 86
  • When I tried this solution and print out the dictionary I got this result. Not sure what isnt working. [('[', "'"), ('c', 'b'), ('1', '3'), ('1', '.'), ('d', 'c'), ('-', '0'), ('1', '.'), ('c', 'o'), ('m', ' '), ('d', 'c'), ('l', 'o'), ('u', 'd'), ('-', 'w'), ('e', 'b'), ('e', 'x'),('-', 'o'), ('r', 'g'), ("'", ','), (' ', "'"), ('1', '4'), ('7', '8'), ('2', '7'), ("'", ','), (' ', "'"), ('c', 'b'), .........it was way longer but didnt have enough characters – John Murray Feb 07 '19 at 22:57
0

You can create an iterator from the list after using ast.literal_eval to parse it from the input text, zip the iterator with itself, and pass the generated sequence of tuples to the dict constructor:

import ast
i = iter(ast.literal_eval(data))
dict(zip(i, i))
blhsing
  • 91,368
  • 6
  • 71
  • 106
  • I tried this solution and I am getting the same output like the solution above when i try to print it out: {'[': "'", 'c': 'o', '1': '4', 'd': 'c', '-': 'o', 'm': ' ', 'l': 'o', 'u': 'd', 'e': 'x', 'r': 'g', "'": ']', ' ': "'", '7': '.', '2': '.', '3': '.', '5': '.', '0': '9', '6': '.','9': '.', '4': '.', '8': '5'} Not sure what I am doing wrong – John Murray Feb 07 '19 at 22:55
  • You should parse the text into an actual list first. I've updated my answer accordingly. – blhsing Feb 07 '19 at 22:59
  • 1
    Perfect, that did the trick. Thank you so much for your quick response!! – John Murray Feb 07 '19 at 23:04
  • I also added a nice print statement to help other folks out here is what i did – John Murray Feb 08 '19 at 11:47