Python Forum
user input for multi-dimentional list without a prior iteration using input()
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
user input for multi-dimentional list without a prior iteration using input()
Hello to my fellow python users,
I am new to python programming and while learning I faced a specific problem which require me to input a multi-dimentional list from user.
I tried using
# input multi-dimensional list from user
l = list(list(input().split())) # this part is not working as I required
print("""in print()""")
print("""in loop""")
for i in l : 

in the hope of inserting multi-dimensional list from the user through input() function but it did't worked.

# input given by user
D 500 W 600

# given output in print() ['D', '500', 'W', '600'] in loop ['D', '500', 'W', '600'] ['D', '500', 'W', '600'] ['D', '500', 'W', '600'] ['D', '500', 'W', '600']
# Expected output in print() [['D', '500'], ['W', '600']] in loop ['D', '500'] ['W', '600']
So, here I am asking my fellow python users to kindly help me with this specific problem if there is a default available because I have tried with row iteration to get the number of rows but that trick is completely separate from what the question generally wants to asks because I already search the google but it didn't give me the required answer. [the number of columns for the inside list is 'n' and the type is <list object>[[<string object>]]]
Kindly, help me to get a solution.
Thank you.
what you have is a double cast, which is the same as a single cast.
that's like saying red, red is the barn colour
and thus both will give you a single dimension list

>>> l = list(list(input().split()))
1 2 3 4 5
>>> l
['1', '2', '3', '4', '5']
>>> l = list(input().split())
1 2 3 4 5
>>> l
['1', '2', '3', '4', '5']
Thank you for your reply.
I know that what I have done but what I am trying to get is a multidimensional list through a one-liner without a prior iteration or in other words from input function something like the following:
If I give input as :
D 500 W 600 S 900
Then, the expected output should be like:
[['D', '500'], ['W', '600'], ['S', '900']]
If there is way for it like how we do normal list input using:
This is not very efficient, but works.
input_list = list(input().split())
result = list(zip(input_list[0::2], input_list[1::2]))
The inner type is a tuple.
My code examples are always for Python >=3.6.0
Almost dead, but too lazy to die:
All humans together. We don't need politicians!
Thank you for your help but is it possible to have inner type also as list? If not then it's okay otherwise your(#DeaD_EyE) answer is fine as I have already tried it in my code and its working. The latter question is for my knowledge.

Thank you all for helping me for my problem.

Thank you!!!
The expectation is, that the user never enters a wrong value or miss a value.
This is also true for the first example I posted.

Inside a list comprehension you can cast a tuple into a list.
By the way, if you don't modify the pairs afterwards, then you don't need to put them into a list.

input_list = list(input().split())
result = [list(pair) for pair in zip(input_list[0::2], input_list[1::2])]
The list comprehension as for-loop:

input_list = list(input().split())
result = []
for pair in zip(input_list[0::2], input_list[1::2]):
I guess the next question is, how to convert the values which are str into int.

input_list = list(input().split())
result = []
for char, value in zip(input_list[0::2], input_list[1::2]):
    result.append([char, int(value)])
If you've more than two fields, there a better method to chunk the data.
The technique is called chunking.

def chunks(iterable, size):
    yield from zip(*[iter(iterable)] * size)

# or if you want lists

def chunks(iterable, size):
    for items in zip(*[iter(iterable)] * size):
        yield list(items)
The package more_itertools have many useful methods.
But first you need to understand Python better.
My code examples are always for Python >=3.6.0
Almost dead, but too lazy to die:
All humans together. We don't need politicians!
Thank you for your help as per my request regarding the question I asked in the previous post and it is very much helpful, I have used your advice in my problem and it really helped me in solving that problem and knowing for solving other problems related to the same.

Thank you #DeadEyE for your help.

Possibly Related Threads…
Thread Author Replies Views Last Post
  I want to check if the input is str or is int & if it's str repeat the loop HLD202 4 125 Nov-23-2020, 11:01 PM
Last Post: perfringo
  input timer Nickd12 9 212 Nov-23-2020, 09:50 PM
Last Post: Nickd12
  Loop back through loop based on user input, keeping previous changes loop made? hbkpancakes 2 136 Nov-21-2020, 02:35 AM
Last Post: hbkpancakes
  Pyspark SQL Error - mismatched input 'FROM' expecting <EOF> Ariean 3 86 Nov-20-2020, 03:49 PM
Last Post: Ariean
  input timer Nickd12 0 120 Nov-18-2020, 12:31 AM
Last Post: Nickd12
  Handling multi-input/output audio in python bor1904 4 322 Nov-04-2020, 08:25 AM
Last Post: CHLOVRL
  calling a function and argument in an input phillup7 3 196 Oct-25-2020, 02:12 PM
Last Post: jefsummers
  list call problem in generator function using iteration and recursive calls postta 1 153 Oct-24-2020, 09:33 PM
Last Post: bowlofred
  Python win32api keybd_event: How do I input a string of characters? JaneTan 3 206 Oct-19-2020, 04:16 AM
Last Post: deanhystad
  Repeating lines of code by an input Josh_Albanos 3 382 Oct-15-2020, 01:04 AM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020