Reputation: 776
I have a list of list1 listoflist = [[],[],[],[],[]]
and want to populate it with data from this function:
def _execute():
while True:
user_input = input("type in: ")
return user_input
What I'm trying to do is to insert data from the input function into the first column, like:
input = 1
[[1],[],[],[],[]]
input = 2
[[1,2],[],[],[],[]]
If the first column length is bigger than 2, then switch to the next column until the last one.
So far I managed to insert data into the first column but it inserts the return value from the input function two times in the first column:
def insertdata(data):
for i in range(len(listoflist)):
if len(listoflist[0]) < 2:
listoflist[0].append(data)
else:
print(listoflist)
break
insertdata(_execute())
# output
type in: 1
[['1', '1'], [], [], [], []]
Do I need to do this operation with indexing ?
1: More like a list of pairs, since the inner list can only store two values.
Upvotes: 1
Views: 129
Reputation: 3511
You can put while True
out of the _execute()
function (every time you return, the while
is bypassed) and keep a flag to tell if your data was added or not, additionally, check the boundaries to avoid index errors:
listoflist = [[], [], [], [], []]
def _execute():
user_input = input("type in: ")
return user_input
def insertdata(data):
data_added = False
i, n = 0, len(listoflist)
while not data_added and i < n:
if len(listoflist[i]) < 2:
listoflist[i].append(data)
data_added = True
else:
i += 1
if i == n:
print("\n The list is full, No more elements will be added \n")
while True:
insertdata(_execute())
print(listoflist)
Upvotes: 2
Reputation: 1162
Maybe it's there for another reason, but the while True
in your _execute()
isn't needed as input
is blocking.
Further I justed commented what your codeblock is doing
def insertdata(data):
for i in range(len(listoflist)): #execute the following code for the amount of lists in listoflist, namely 5 times
if len(listoflist[0]) < 2: #if you have less than 2 elements in the first element of listoflist
listoflist[0].append(data) # append data to the first element
else:
print(listoflist)
break
for a correct function, see RMPR's answer.
Upvotes: 1
Reputation: 21275
You just need to find the first list in the list of lists with space to handle the user input. After adding the user input, you can just return - there is nothing else to do. You are getting the double addition because the append()
is in a for-loop
You should also handle the case when no more elements can be added:
listoflist = [[],[],[],[],[]]
def _execute():
while True:
user_input = input("type in: ")
return user_input
def insertdata(data):
for l in listoflist:
if len(l) < 2:
l.append(data)
return
else:
print("no more space")
while True:
insertdata(_execute())
print(listoflist)
Output:
type in: 1
[['1'], [], [], [], []]
type in: 1
[['1', '1'], [], [], [], []]
type in: 1
[['1', '1'], ['1'], [], [], []]
type in: 1
[['1', '1'], ['1', '1'], [], [], []]
type in: 1
[['1', '1'], ['1', '1'], ['1'], [], []]
type in: 1
[['1', '1'], ['1', '1'], ['1', '1'], [], []]
type in: 1
[['1', '1'], ['1', '1'], ['1', '1'], ['1'], []]
type in: 1
[['1', '1'], ['1', '1'], ['1', '1'], ['1', '1'], []]
type in: 1
[['1', '1'], ['1', '1'], ['1', '1'], ['1', '1'], ['1']]
type in: 1
[['1', '1'], ['1', '1'], ['1', '1'], ['1', '1'], ['1', '1']]
type in: 1
no more space
[['1', '1'], ['1', '1'], ['1', '1'], ['1', '1'], ['1', '1']]
Upvotes: 0