1

So I have multiple lines input and I want to create a list for each line EX Input:

5 2
6 1
7 3
4 2
10 5
12 4

EX output

[5, 2]
[6, 1]
[7, 3]
[4, 2]
[10, 5]
[12, 4]

please help, thanks

I have try create with for i in rage but didn't work

lst=[]
n, s = [int(y) for y in input().split()]
for i in range(n):
    lst = list(map(int, input().split()))
count = 0
print(lst)
Talha Tayyab
  • 8,111
  • 25
  • 27
  • 44
VKUOJ
  • 19
  • 1
  • 1
    You are overwriting `lst` in every iteration. Do you need a list of list? – kuro Mar 04 '23 at 09:02
  • 1
    no, i just need multiple list each contain a input line – VKUOJ Mar 04 '23 at 09:05
  • 2
    @VKUOJ How do you expect to store those "multiple lists" if not in a list? Do you want each line in a different variable? Do you just want to print each line and then forget it? Do you want to iterate over each line, calling a function with the line as parameters, and then forget it? Or, do you want to store all of the lines (as a separate list per line) in a single variable? (Which would be a list, of those lists...) – MatBailie Mar 04 '23 at 09:23

4 Answers4

1

Is this what you're looking for?

# Initialize an empty list to store sublists
lists = []

# Get the number of lists and the size of each list
n = int(input()) # Add appropriate input message

# Iterate over each list and append it to the sublists list
for i in range(n):
    sublist = list(map(int, input().split()))
    lists.append(sublist)

# Log each sublist to console in new line
print(*lists, sep='\n')

orimiles5
  • 121
  • 4
  • That would work if we knew the number of lines, but not if it was a variable length – Cornelius-Figgle Mar 04 '23 at 09:12
  • 1
    @Cornelius-Figgle But the OP does include a line where they attempt to initialise `n` from an `input()`. We don't really have any idea what's going on, the OP is incredibly ambiguous. – MatBailie Mar 04 '23 at 09:41
1

If you just append the values from your initial input line to lst it allows you to take each line and make it into a 2D list

>>> lst = []
>>> lst.append([int(y) for y in input().split()])
5 2
>>> lst.append([int(y) for y in input().split()])
6 1
>>> lst
[[5, 2], [6, 1]]
>>> 

However, you mentioned multiple lines so I assume you want to take all the input at once. Using this answer, I adapted the code to append each line as a list (as above). Input will only stop when a Ctrl-D (posix) or Ctrl-Z (windows) character is found

>>> lst = []
>>> while True:
...     try:
...         line = input()
...     except EOFError:
...         break
...     lst.append([int(y) for y in line.split()])
... 
5 2
6 1
7 3
4 2
10 5
12 4
^Z
>>> lst
[[5, 2], [6, 1], [7, 3], [4, 2], [10, 5], [12, 4]]
>>> 

Hope this helps!

EDIT: Thought I would add, this could then be used later by iterating over the outer list

>>> for item in lst:
...     print(item)
... 
[5, 2]
[6, 1]
[7, 3]
[4, 2]
[10, 5]
[12, 4]
>>> 
  • it only print [12,4] – VKUOJ Mar 04 '23 at 09:24
  • What do you mean? It works fine for me, can you please add some clarity to explain the problem – Cornelius-Figgle Mar 04 '23 at 09:25
  • @VKUOJ How is the input being supplied? Are you typing the lines in by hand, pasting the entire set as a single block? Or something else? *(Please update your question with this missing information.)* – MatBailie Mar 04 '23 at 09:36
  • 2
    @Cornelius-Figgle I think you need to assume that the data *can* also be pasted in as a multiline block, or the end-of-lines be escaped with a \ character, etc, etc. So, iterate over `input().splitlines()` first? That way, it works for either scenario. – MatBailie Mar 04 '23 at 09:52
  • @MatBailie I wrote it pasted in, but idk ab the `\` escapes. Are you saying to iterate over `input().splitlines()` to check if its alr split (ie escape chars)? It might just be my terminal but pasting in multilines does the same as typing them out, ie only the first line in captured by `input()` rather than all of it? Hope that makes sense - I think you are saying to check the number of lines in the first input, and only doing the loop thing if only one line was intially entered? – Cornelius-Figgle Mar 04 '23 at 13:07
1

One Liner:

[[int(x) for x in input().split()] for i in range(6)]

5 2
6 1
7 3
4 2
10 5
12 4

#[[5, 2], [6, 1], [7, 3], [4, 2], [10, 5], [12, 4]]

You can generalize 6 to n in range

Here, I am using a list comprehension instead of list(map(int, input().split())).

A link to document

https://python-reference.readthedocs.io/en/latest/docs/comprehensions/list_comprehension.html

Talha Tayyab
  • 8,111
  • 25
  • 27
  • 44
0

Supposing you want a list of list where inner list contains the values in each row, you need to change the for loop like -

for i in range(n):
    lst.append(list(map(int, input().split())))
print(lst)

The output will be like -

[[6, 1], [7, 3], [4, 2], [10, 5], [12, 4]]

In your code you are overwriting the lst with a list containing the values of each row. SO after the loop finishes, lst contains only the last row values.

If your intention is to print each row as a list, you can put print inside loop and remove lst altogether.

for i in range(n):
    print(list(map(int, input().split())))

Output:

[6, 1]
[7, 3]
[4, 2]
[10, 5]
[12, 4]
kuro
  • 3,214
  • 3
  • 15
  • 31