Append multiple values for a key from a file
<p>I've got a file similar to below:</p>

<pre><code>t_air_sens1
laten
t_air_sens1
periodic
t_air_air
laten
t_air_air
periodic
...
...
</code></pre>

<p>I want to make a dictionary in order to assign those values of <em>laten</em> and <em>periodic</em> to each key of <em>t_air_sens1</em> and etc. The final result must be something like below:</p>

<pre><code>{
    "t_air_sens1": [laten,periodic]
    "t_air_air": [laten,periodic]
     ...
     ...
}
</code></pre>

<p>I did write the code below:</p>

<pre><code>prop_dict = {}
with open('file.pl') as f, open('result.pl', 'w') as procode:
     for line in f:
         if line[0] in prop_dict:
            prop_dict[line[0]].append(line[1])
         else:
            prop_dict[line[0]] = [line[1]]
         #will write the values in "result.pl"
</code></pre>

<p>But the result I get when I try to print the dictionary is something like below:</p>

<pre><code>{'p': ['e', 'e', 'e', 'e', 'e', 'e', 'e', 'e', 'e', 'e', 'e', 'e'],
 't': ['_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_',
       '_', '_', '_', '_', '_', '_', '_', '_', '_'],
 'l': ['a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a']}
</code></pre>

<p>What can I do to fix it? How do I need to make queries in order to get the proper result?</p>

<p><code>line[0]</code> and <code>line[1]</code> are single characters in the string, not the current line and the next.</p>

<p>File objects are iterators; the <code>for</code> loop will get new lines from it each iteration, but you can also use the <a href="https://docs.python.org/3/library/functions.html#next" rel="nofollow"><code>next()</code> function</a> to pull another line in. Use this to read two lines at a time:</p>

<pre><code>prop_dict = {} 
with open('file.pl') as f:
     for key in f:
         key = key.rstrip('\n')
         # get the next line as the value
         value = next(f).rstrip('\n')
         prop_dict.setdefault(key, []).append(value)
</code></pre>

<p>I also used <a href="https://docs.python.org/3/library/stdtypes.html#dict.setdefault" rel="nofollow"><code>dict.setdefault()</code></a> to insert an empty list for any key that is missing from the dictionary. Like <code>prop_dict[key]</code>, it'll return the current value in the dictionary, but if there is no such key, <code>prop_dict[key] = []</code> is executed first before returning that empty list.</p>

<p>How the above works is that the <code>for</code> loop reads lines one by one as the loop iterates, basically by using <code>next()</code> internally. Calling <code>next(f)</code> in the loop simply draws in an extra line, and the <code>for</code> loop continues from there again, so you are alternating between reading a property name (<code>key</code>), and a property value (<code>value</code>).</p>

<p>Note that <code>next()</code> can raise a <code>StopIteration</code> exception if the end of the file was reached by reading the last line in the <code>for</code> loop; this would indicate your file did not have an even number of lines. If this is not an error, you could specify a default instead: <code>next(f, '')</code> would return the empty string <code>''</code> if the file has been exhausted.</p>

python dictionary key-value