Seems simple, yet elusive, want to build a dict from input of [key,value] pairs separated by a space using just one Python statement. This is what I have so far:
Assuming you have the text in variable s
:
dict(map(lambda l: l.split(), s.splitlines()))
for i in range(n):
data = input().split(' ')
d[data[0]] = data[1]
for keys,values in d.items():
print(keys)
print(values)
using str.splitines()
and str.split()
:
In [126]: strs="""A1023 CRT
.....: A1029 Regulator
.....: A1030 Therm"""
In [127]: dict(x.split() for x in strs.splitlines())
Out[127]: {'A1023': 'CRT', 'A1029': 'Regulator', 'A1030': 'Therm'}
str.splitlines([keepends]) -> list of strings
Return a list of the lines in S, breaking at line boundaries. Line breaks are not included in the resulting list unless keepends is given and true.
str.split([sep [,maxsplit]]) -> list of strings
Return a list of the words in the string S, using sep as the delimiter string. If maxsplit is given, at most maxsplit splits are done. If sep is not specified or is None, any whitespace string is a separator and empty strings are removed from the result.
This is what we ended up using:
n = 3
d = dict(raw_input().split() for _ in range(n))
print d
Input:
A1023 CRT
A1029 Regulator
A1030 Therm
Output:
{'A1023': 'CRT', 'A1029': 'Regulator', 'A1030': 'Therm'}