And you can remove
zip
and
*
too and use
dict([line.split(':')])
If
dict()
gets only one unnamed argument, it must be either mapping or iterable containing pairs
key, value
. In your case
line.split(": ")
returns list of two items ("pair"), so it must be "put" into some iterable - it can be done by creating list or tuple containing that pair. Wavic's unpacking and zipping works for this specific keys/values, but try it for line
AB:CD
or
A:BC
.
key, value = line.split(":")
my_dict = {key: value}
is probably more beginner friendly and more readable way to create dict.
Anyway this will always create new dictionary for each line. If you want to have same dictionary with all values, you need to either "collect" all key: value pairs and then convert them into dict at once, or create dictionary and update it for each line. Second way:
my_dict = {}
with open('file.txt') as fileobj:
for line in fileobj:
key, value = line.split(":")
my_dict[key] = value
Likely you would want to use
.strip()
to remove end of lines ("\n").