How to create a list of key:value pairs in a for loop?
17,800
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']"
Author by
ShanZhengYang
Updated on June 04, 2022Comments
-
ShanZhengYang almost 2 years
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 over 7 yearsSo, 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 -
skovorodkin over 7 years@ShanZhengYang Do you want to get a list of dicts?
['key1': 'value1']
is not legal Python syntax. -
ShanZhengYang over 7 yearsI 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 textkey1:value1 key2:value2 key3:value3
... -
skovorodkin over 7 years@ShanZhengYang So you just want to make a string, not Python data structure?
-
ShanZhengYang over 7 yearsThat 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? -
skovorodkin over 7 years@ShanZhengYang check out the latest update of the answer.
-
ShanZhengYang over 7 yearsThanks 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? -
skovorodkin over 7 years@ShanZhengYang, that's a completely different question.
-
ShanZhengYang over 7 yearsYou're right---I'll ask in another thread. Thanks for the help!