0

Solved

My exercise is to write a function called list_check, which accepts a list from the user and returns True if every element in the userlist is itself also a list.

The bottom line is I would like to see a working example of this problem solved using user input, which is more difficult than just supplying the list yourself.

This is the closest I’ve come accepting the user’s input for the list:

userlist = []
number_of_elements = int(input("Enter the number of elements in your list: "))

for i in range(0, number_of_elements):
    element = input().split()
    userlist.append(element)

if all(isinstance(element, list) for element in userlist):
    print("True")
else:
    print("False")

The working code without user input is as follows:

customlist = [[1,2,3],[2,3,4], False]

def list_check(customlist):
    answer = all(type(l) == list for l in customlist)
    print(answer)

list_check(customlist)

False

Appreciate the help. - J

  • 2
    `input()` will always return a string. So instead of having a list, you have a string representation of a list, i.e. `"[1, 2, 3]"`. – Selcuk Jul 26 '19 at 00:00
  • You can change it to `element = input().split()` – Samik Jul 26 '19 at 00:06
  • I’m still having trouble with the last part of the code. I changed it to an if statement : if all(isinstance(element, list) for element in userlist): print(True) else: print(False) – jonthesupreme Jul 26 '19 at 00:18
  • I posted an answer, but why are you printing the string True and False rather than the boolean? I'm not sure if you know this, but range(0, t) == range(t). 0 is the default starting integer. – Elijah Jul 26 '19 at 01:52
  • You're supposed to be writing a function, with a list as an argument and a boolean as a return value. You're not supposed to be taking keyboard input or printing to the console. – user2357112 Jul 26 '19 at 01:59
  • So user2357112, for sure - me trying to take the user’s input caused alot of extra confusion, and without user input the problem is much easier. However, I’d still like to see a clear example of the problem solved using user input. Eli, Thank you for your input I will be checking out your code. – jonthesupreme Jul 26 '19 at 02:27

2 Answers2

0
def listcheck():
    y = (input("Enter your lists: \n"))
    if y[0] !="[" or y[1] !="[":
        print("false, you entered data not starting with [[")
        return False
    if y[len(y)-1] !="]" or y[len(y)-2] !="]":
        print("false, you entered data not ending with ]]")
        return False
    import ast
    z = ast.literal_eval(y)
    def innerlistcheck(alist):
        for x in range(0, len(alist), 1):
            if type(alist[x]) != list:
                print("false, " + str(alist[x]) + " is not a list")
                return False
        print("true")
        return True
    innerlistcheck(z)

listcheck()

I think this may be an answer to your question. The hardest part was seeing how to convert a string to a list which I stole from here: Convert string representation of list to list

sanvicente
  • 26
  • 3
  • My answer is a function that when called asks for user input. The input, as Selcuk helped me realize, is a string. Before I convert the input to a list I check to see if it started with "[[" and ended with "]]", since every list of lists should have that format. – sanvicente Jul 26 '19 at 14:35
  • Definitely a step in the right direction. I wouldn't say this solution is perfect though, as I tested it using the following input(s): [[2,3,4],[5,3,2],True] , as well as [[2,3,4],[5,3,2],True]] , and both received errors. Thanks for the code though I'll be tweaking it. Meantime I will leave the thread open. – jonthesupreme Jul 26 '19 at 14:58
  • [[2,3,4],[5,3,2],True]] is bad syntax because it has an extra "]". – sanvicente Jul 26 '19 at 15:42
  • [[2,3,4],[5,3,2],True] should print "false, you entered data not ending with ]]" and return False ? What is the error you get when you run that? – sanvicente Jul 26 '19 at 15:45
  • I got confused because the error was talking about brackets as opposed to non-list data types. I suppose that is technically correct. And I could adjust the wording of the error. Thanks! – jonthesupreme Jul 27 '19 at 00:57
  • Yay! Glad it matched what you were looking for! – sanvicente Jul 27 '19 at 04:01
-1

That is because .split() will always return a list. 'dog'.split() == ['dog'].

Solution (Optimized)

number_of_elements = int(input("Enter the number of elements in your list: "))

output = True
for _ in range(number_of_elements):
    element = input().split()
    if len(element) == 1: output = False

print(output)
Elijah
  • 1,814
  • 21
  • 27