Append Values from input to a sublist in Python
<p>I am trying to append values from an input to sublists in a list.
Each number of Student and name should be in a sublist.
ex: </p>

<pre><code>[[123,John],[124,Andrew]]
</code></pre>

<p>Where the outside list would be the number of students, and the sublists , the info of the students..</p>

<p>Here is what my code looks like:</p>

<pre><code>listStudents = [[] for _ in range(3)]
infoStudent = [[]]

while True:
    choice = int(input("1- Register Student 0- Exit"))
    cont = 0
    if choice == 1:
            snumber = str(input("Student number: "))
            infoStudent[cont].append(str(snumber))
            name = str(input("Name : "))
            infoStudent[cont].append(str(name))
            cont+=1
            listStudents.append(infoStudent)
    if choice == 0:
        print("END")
        break


print(listStudents)

print(infoStudent)
</code></pre>

<p>If I put on the first loop, <code>snumber = 123</code> , <code>name = john</code> , and <code>snumber = 124</code>, <code>name = andrew</code> on the second time it will show me : <code>[[123,john,124,andrew]]</code> instead of <code>[[123,john], [124,andrew]]</code>.</p>

<p>Your code can be greatly simplified:</p>

<ol>
<li>You don't need to pre-allocate the lists and sublists. Just have one list, and append the sublists as you receive inputs.</li>
<li>You don't need to cast user input from <code>input</code> to strings, as they are strings already.</li>
</ol>

<p>Here's the modified code:</p>

<pre><code>listStudents = []

while True:
    choice = int(input('1- Register Student 0- Exit'))
    if choice == 1:
        snumber = input('Student number: ')
        name = input('Name : ')
        listStudents.append([snumber, name])
    if choice == 0:
        print('END')
        break

print(listStudents)
</code></pre>

python list append sublist