I would like to define a dictionary using the 2 first columns of a text file (with a tab delimiter).
Using the following code, I create a dictionary "dict" in which the keys are the rows in the first column and the values are the rows in the second and third columns:
mydict = {}
with open("myfile.txt") as f:
mydict = dict(x.split(" ", 1) for x in f)
However, I would like to discard the third column and use the first column as a key and the second as a value in the dictionary "dict". I tried this code but it does not work for me:
mydict = {}
with open("myfile.txt") as f:
mydict = dict(x.split(" ", 2)[0:1] for x in f)
Any suggestions are welcome, thanks!