 <pre><code>dict={} x1=fileobject.read() for line in x1.splitlines(): if line.split()[1] in dict.keys(): dict[line.split()[0]]=line.split()[1]+" "+dict[line.split()[1]] else: dict[line.split()[0]]=line.split()[1] print dict </code></pre> <p>This way you can have a dictionary of object with keys as you want.</p> <p>Output:<code>{'k3': 'v3', 'k2': 'v2', 'k1': 'v1', 'k5': 'k4 k1 v1', 'k4': 'k1 v1'}</code></p>