How to take multiple sets of inputs separated by space and carriage return in Python 3?
<p>So we are allowing a user to enter the number sets they want to enter. Each set of input will contain two integers separated by space. Then, the carriage return denotes the next set of inputs. For example,</p>

<pre><code>Enter number of sets: 3
1 3
2 4
5 6
</code></pre>

<p>Next we input these in variables a,b, perform same operations, display 3 results:</p>

<pre><code>4
3
1  
</code></pre>

<p>It should first take all inputs <em>and then</em> show all respective outputs. 
We have the logic of processing 1 set of input, but how do we loop it so that we can accept input in this format?</p>

<pre><code>sets = int(input("Enter number of sets: "))
inputs = []
for n in range(sets):
    inputs[n] = int(input().strip())
</code></pre>

<p>This crashes with list out of range error. We were thinking of creating a list of lists to hold the pair of values. Any easier solutions?</p>

<p>EDIT: What I'm looking for is a way to solve this problem. It doesn't have to be done via lists specifically. It is not a generic list out of range problem. I do understand what is going wrong, I just need another way to do it.</p>

<p>The <code>input()</code> is returning a string like <code>"1 3"</code>.  Parse that string with something like, <code>a, b = map(int, input().split())</code>.  Save the output by using <em>list.append()</em>.</p>

<pre><code>from pprint import pprint

inputs = []
results = []
sets = int(input("Enter number of sets: "))
for n in range(sets):
    s = input()
    a, b = map(int, s.split())
    result = a + b
    inputs.append([a, b])
    results.append(result)
pprint(inputs)
pprint(results)
</code></pre>

<p>A sample session looks like this:</p>

<pre><code>Enter number of sets: 3
1 3
2 4
5 6
[[1, 3], [2, 4], [5, 6]]
[4, 6, 11]
</code></pre>

<p>The learning points are:</p>

<ul>
<li>Use <a href="https://docs.python.org/3.5/library/stdtypes.html#str.split" rel="nofollow">str.split()</a> to convert a string like <code>"1 3"</code> into a list like <code>['1', '3']</code></li>
<li>Use <a href="https://docs.python.org/3.5/library/functions.html#map" rel="nofollow"><em>map()</em></a> with <a href="https://docs.python.org/3.5/library/functions.html#int" rel="nofollow"><em>int()</em></a> to convert <code>['1', '3']</code> to <code>[1, 3]</code></li>
<li>Use <a href="https://docs.python.org/3.5/tutorial/controlflow.html#unpacking-argument-lists" rel="nofollow">variable unpacking</a> to extract the two values</li>
<li>Use <a href="https://docs.python.org/3.5/library/stdtypes.html#mutable-sequence-types" rel="nofollow"><em>append()</em></a> to grow the <em>inputs</em> and <em>results</em> lists</li>
</ul>

python list user-input python-3.4