user1834857
user1834857

Reputation:

How can I make a list of lists from user input?

Here is what I did-

grid_len = input("Enter Grid Length: ") #Assuming grid_length to be 3
s = []
while True:
    s.append(input())
    if len(s) == int(grid_len)**2: #grid_length^2 will be 9
        print(s)
        break

When Input is for example 1 in the first loop, 2 in the second, 3 in the third and so on upto 9; It creates a list like this:

['1','2','3','4','5','6','7','8','9']

But I want it something like this:

[[1,2,3],[4,5,6],[7,8,9]]

Upvotes: 3

Views: 11950

Answers (10)

Here is my code. Using this we can have different sizes of lists inside the list:

len_of_list = int(input('Enter the first layer list size: '))
list_of_list = []


for i in range(len_of_list):
  len_of_elem_inside_list = int(input('Enter the second layer list sizes: '))
  inside_list =[]

  for j in range(len_of_elem_inside_list):
    elements_in_the_list = int(input('Enter the elements inside the list of list: '))
    inside_list.append(elements_in_the_list)

  list_of_list.append(inside_list)
print('Output: ',list_of_list)

Upvotes: 0

SidJ25
SidJ25

Reputation: 47

Try this:

grid_len = int(input()) #just one input
grid = [[input(),input(),input()] for _ in range(grid_len)] #grid_len * 3 inputs
print(grid)

Upvotes: 0

coderina
coderina

Reputation: 1746

I have got your problem, the very simple solution is

grid_length = int(input())
s = []
for i in range(grid_length):
     b = list(map(int, input().split()))
     s.append(b)
print(s)

Upvotes: 1

Amir
Amir

Reputation: 111

Try this:

arr = [list(map(int, input().split())) for i in range(int(input()))]

Upvotes: 1

ShuklaSannidhya
ShuklaSannidhya

Reputation: 8976

Here's my code:

grid_len = input("Enter Grid Length: ")
s = []
for i in range(grid_len):         #looping to append rows
    s.append([])                  #append a new row
    for j in range(grid_len):     #looping to append cells
        s[-1].append(input())     #append a new cell to the last row, or you can also append to `i`th row

Upvotes: 1

Bakuriu
Bakuriu

Reputation: 101959

You should create a new sub-list every grid_length elements:

grid_len = int(input("Enter Grid Length: "))
s = []
for _ in range(grid_length):
    sub_list = []
    for _ in range(grid_length):
        sub_list.append(input())
    s.append(sub_list)
print(s)

Note that, in general, you should use for every time that you have to iterate sequentially over an object or you know how many times to repeat a loop. while is generally better to handle "strange" conditions that are hard to factor in terms of number of iterations or iterating over an iterable.

Upvotes: 0

pradyunsg
pradyunsg

Reputation: 19416

Try this:

x = [[int(input()) for c in range(grid_len)] for r in range(grid_len)]

Upvotes: 0

TerryA
TerryA

Reputation: 59974

Something I found from this question: How do you split a list into evenly sized chunks?

>>> mylist = [1,2,3,4,5,6,7,8,9]
>>> def chunks(l, n):
...    return [l[i:i+n] for i in range(0, len(l), n)]
>>> chunks(mylist,3)
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]

Integrated into your code:

def chunks(l, n):
    return [l[i:i+n] for i in range(0, len(l), n)]
grid_len = input("Enter Grid Length: ")
s = []
while True:
    s.append(int(input())) # Notice I put int() around input()
    if len(s) == int(grid_len)**2:
        s = chunks(s,grid_len)
        print(s)
        break

EDIT: Changed the second parameter in chunks to match grid_len. This will now work for not just 3.

Upvotes: 2

johnsyweb
johnsyweb

Reputation: 141810

Using nested list comprehensions:

>>> grid_len = input("Enter Grid Length: ")
Enter Grid Length: 4
>>> incrementer = iter(xrange(1, grid_len ** 2 + 1))
>>> s = [[next(incrementer) for x in xrange(grid_len)] for y in xrange(grid_len)]
>>> print s
[[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]]

Upvotes: 0

Ifthikhan
Ifthikhan

Reputation: 1474

A version based on list comprehension.

s = [[input("Enter number: ") for _ in range(grid_len)] for _ in range(grid_len)]
print s

Note: Two forward slashes "//" are not valid python comment identifiers

Upvotes: 5

Related Questions