2

I have a for loop which outputs a list of 15 values:

for line in data: 
    line # type(line) is `<class 'list'>`
        # do things with line

This line is always a Python list of 11 values, i.e.

['value1', 'value2', 'value3', 'value4', ... ] 

I have a list of dictionary keys, which I have manually created:

['key1', 'key2', 'key3', ...]

I would like to join my keys to each of the values for each line in my for loop. Therefore, I can define a new line that looks like

 ['key1': 'value1', 'key2': 'value2', 'key3': 'value3', ... ]

This would be used in the for loop, i.e.

for line in data: 
    line # values
    # join key-value pairs
    # new_line = ['key1': 'value1', 'key2': 'value2', 'key3': 'value3', ... ]
        # do things with new_line

EDIT: I don't believe this is a repeat question. zip(keys, values) does not produce the desired result.

EDIT2: Would it be easier to create one large dictionary within a list?

[{'key1': 'value1', 'key2': 'value2', 'key3': 'value3', ... }]

or perhaps a tab-delimited test file?

key1:value1 key2:value2 key3:value3 ....

ShanZhengYang
  • 16,511
  • 49
  • 132
  • 234

1 Answers1

8

If you just want to make a dict from two lists, then use zip function:

>>> keys, values = ['a', 'b'], [1, 2]
>>> list(zip(keys, values))
[('a', 1), ('b', 2)]

>>> dict(zip(keys, values))
{'a': 1, 'b': 2}

>>> [{k: v} for k, v in zip(keys, values)]
[{'a': 1}, {'b': 2}]

>>> ' '.join('{}:{}'.format(k, v) for k, v in zip(keys, values))
'a:1 b:2'

>>> '[{}]'.format(', '.join("'{}': '{}'".format(k, v) for k, v in zip(keys, values)))
"['a': '1', 'b': '2']"
skovorodkin
  • 9,394
  • 1
  • 39
  • 30
  • So, it appears neither of the above options solves my problem. My output actually looks like `['key1': 'value1', 'key2': 'value2', 'key3': 'value3', ... ]`. Not `[('a', 1), ('b', 2)]` nor `{'a': 1, 'b': 2}`. It's a list of key-value pairs – ShanZhengYang Oct 05 '16 at 12:12
  • @ShanZhengYang Do you want to get a list of dicts? `['key1': 'value1']` is not legal Python syntax. – skovorodkin Oct 05 '16 at 12:17
  • I have quite a bit of data in the format ['key1:value1', 'key2:value2',...] which which I was going to use partition(:), etc. I wanted the vectors above to be in the same format. It is possible to create one big dicitonary in a list, i.e. `[{'key1:value1', 'key2:value2',...}] `, or maybe even take the key-value pairs to make a tab-delimited text `key1:value1 key2:value2 key3:value3 `... – ShanZhengYang Oct 05 '16 at 12:28
  • @ShanZhengYang So you just want to make a string, not Python data structure? – skovorodkin Oct 05 '16 at 12:31
  • That make be easier. (1) how to use key list and value list to make a key:value string (as in previous comment) and (2) how to turn a large dictionary in a list like `[{'key1:value1', 'key2:value2',...}]` into a key:value string? – ShanZhengYang Oct 05 '16 at 12:33
  • @ShanZhengYang check out the latest update of the answer. – skovorodkin Oct 05 '16 at 12:35
  • Thanks for this! I'm trying to parse this weird format so it is something standard. The last thing is I still have this: `[{'key1:value1', 'key2:value2', 'key3:value3',...}]` That's the only data I have, and it needs to be processed. I don't think this can be described as a Python data structure, but I need to parse this to become a string like 'a:1 b:2'. Is this doable? – ShanZhengYang Oct 05 '16 at 12:40
  • @ShanZhengYang, that's a completely different question. – skovorodkin Oct 05 '16 at 12:42
  • You're right---I'll ask in another thread. Thanks for the help! – ShanZhengYang Oct 05 '16 at 12:48