List input from user


If I decide to take the list as an input from the user for any of the program and use it later as we do for any of our functions, how can I do that?
Besides I am not knowing the size of the list the content or the data type the user will enter in the list, and lastly as a user how should I enter a list, that is, if I hit enter after an element then it will terminate the list, otherwise, if I can use space to seperate the elements then there could be a possibility that on of the element could have space as a part of its content.


def list_function(lst):
    # given a list do something with it

What can we do with a list? A lot. A list is a data structure, meaning it houses indexed and iterable data elements. Those are the two operative words,.. indexed and iterable.

indexed means we can access each member, discretely.

iterable means we can iterate the object sequentially, or in any order feasible.

A lot of your question relates to user inputs validation. That is up to our program to look after. Since functions should rightly do only one thing, it follows that we would write separate validation functions that are run on the inputs to the function at hand. You program could have,

>>> def is_list(x):
    return isinstance(x, list)

>>> is_list(['e', 's', 'o', 'o', 'm'])

Now our function can validate the inputs:

def foo(d):
    if not is_list(d): return "Input Error"

That is only one approach. There are many. Strict coding could well be do prequalifying of all call arguments as a means of protecting the functions from the outside, but this would still expose the functions themselves. A little of both is needed, one would congecture.

I sort of follow this but not quite. Allow a suggestion concerning dressing element data. Don't. Data should be left as is, where is. in the raw with no leading or trailing spaces on strings.

We can insert spaces between strings using the .join() function which is a string tool that converts a list to a string, or inserts characters into an already string object.


Although this was helpful, can you give me an example of what exactly the code would be, to ask the user to input the list and how the console would look when the user enters the list on prompted by the program.

Thanks a lot


Again, keeping to a rule that a function does only one thing,

def get_list():
    d = ''
    while not is_list(d):
        d = raw_input("Enter a list: ")
    return d

user = get_list()

Here we have control over the data moving forward. Front line defense. With our other function running the same test on its end, we have a double whammy. I'm sure you can work with some of these ideas on your own. Give it a try.


When a list is passed to a function, it could be serving any one of a number of specialized roles. It could be a list of arguments, a, b, c to fit individual conditions, or to be iterated over and fit to the same condition, each time. We might wish to do math on all the elements in the list, or with all the elements.

Lists can be homogeneous which means each element has independence of type. The list could contain two numbers, a couple of strings, a dictionary and a couple of lists. An element may even be a function (as a lambda) though that is awkward. A class method is more the thing for that.

It's rather clear by now that the author designs the data structures and the means to incorporate them in the various processes. What is it we wish to do? With what kind of data? What do we expect in the results?


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.