0

How can we take input of n number of lists in python

for example

2
1 2 3
4 5 6 7

here 2 is specifying number of lists that are going to input

1 2 3 is one list

4 5 6 7 is second list

another example

3
1 2 3
4 5 6 8
2 3 5 7

3 indicates 3 lists are going to input

1 2 3 is list one
4 5 6 8 is list two
2 3 5 7 id list three

i have done this code

n=input()
for i in range(n):
    b=map(int,raw_input().split())

i am struck with this how can i take input for n number of lists i able to take only one list into one variable i want take to different different variables

6
  • 3
    Please show your effort. What's the code you've come up with so far? Commented Nov 9, 2017 at 15:39
  • 1
    Seems like there may be some XY problem going on here Commented Nov 9, 2017 at 15:42
  • 3
    make an empty list before the loop and append each map to it instead of assigning them to a variable Commented Nov 9, 2017 at 15:45
  • 1
    The only real problem with your code so far is that b gets overwritten each time through the loop; you aren't accumulating your lists in a second, outer list yet. Commented Nov 9, 2017 at 15:45
  • i have edited my question and add my little code can you please check Commented Nov 9, 2017 at 15:45

3 Answers 3

1

i want take to different different variables

You can not assign the input to "different" variables in the loop, particularly if you do not know how large n will be. Instead, you should append the different values of b to a list of lists, e.g. bs.

n = input()
bs = []
for i in range(n):
    bs.append(map(int, raw_input().split()))

Or use a list comprehension:

bs = [map(int, raw_input().split()) for _ in range(n)]
Sign up to request clarification or add additional context in comments.

2 Comments

instead of append all list to one list can't we take to different different variables
@saivinaymanapuram No, you can't, and there is really no good reason you would want to. This looks like some sort of programming excercise (Hackerrank, maybe?), and you will probably want to do the same operation for each of those lists. So putting them in a list is much better than having many differently named variables.
0

do you want to read from file or cli?

If you read from file, you can iterate over its content line by line and work only in the specific lines.

The lines input you can split to get the single numbers into a list.

nums=[]
with open(infile.txt) as f:

 n=0
 for i, line in enumerate(f):
     if i==1:
          n == 1
     elif i <= n
         nums[i] = line.split()

Comments

0

There are a few things to fix:

  1. You need to convert your input 'n' to an integer.
  2. Your 'b' gets overwritten with every iteration of the loop.
  3. Why are you using 'raw_input' in the second case? 'input' will give you a sanitized version of the input which is preferable in this case (so that a malicious user cannot inject code).
  4. Keep it simple and use lists instead of a map.

    
    n = int(input())
    rows = [] # will contain the input as a list of integers
    for i in range(n):
        row_string = input()
        row = [int(num) for num in row_string.split()]
        rows.append(row)
    

2 Comments

It seems like you are confusing what input and raw_input do. raw_input only exists in Python 2 (thus that's what OP seems to use), where it does the same thing as input in Python 3, whereas input in Python 2also evaluates the input, thus no cast to int needed.
@tobias_k thanks for the feedback. I am aware of this but isn't it usually an unsafe practice to literally evaluate input? If the code snippet is the full extent of the usage then maybe there is no risk but what about if this snippet was used in a bigger software? Is the risk of injection worth it?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.