def get_pair(line): key, sep, value = line.strip().partition(" ") return int(key), value with open("file.txt") as fd: d = dict(get_pair(line) for line in fd)