Enter the user's input in the n-inner lists | Sololearn: Learn to code for FREE!


Enter the user's input in the n-inner lists

I am trying to figure out, how would it be possible to insert in an input number of inner lists of the list different users inputs? n = int (input ("Please enter a number:")) L = [[] for i in range (n)] for i in range (n): L[0:n].append (input ("Please enter 3 words or letters or numbers:")) For example, user gives n = 3. Then the user is supposed to receive three times the question to enter a string. All three strings should enter accordingly each one into a separate inner list. Three days of battle with this thing... Probably still dont understand how the Loop iteration works. :(

12/28/2018 11:10:17 AM


5 Answers

New Answer


Did you mean something like this: some_list=[ [] for i in range(n) ] for i in range(n) : some_list[i].append(input('Enter a string here: ')


I'd do it like this: inner_list = [] for i in range(n): inner_list.append([input()]) L.append(inner_list) (Or the same model in list comp shape.) One question: Why don't you just append the user inputs? Is it necessary to put them into an extra list? Without it, an inner list might look like this: ['first', 'second', 'and third input'] And one opinion: Usually it would be more convenient to let the user decide on the fly, how many inputs they want to do. That could look like this: inner_list = [] while True: x = input() if not x: break inner_list.append(x) L.append(inner_list) In this pattern, the user keeps appending as long as they like, but when they just hit enter, the list gets appended to the larger list.


Thank you! The second option is understanble for me and it works. Another question has popped up: the user enters a string like "abc", can i slice it directly into the inner list as three strings "a", "b", "c"? And the second string "def" into "d", "e", "f" etc?


You can split any string by just using list(string).


Since 'Strings' are iterable in Python so you can use extend method of list to add each character of a string into a list or you can convert it directly into a list as above said by HonFu.