Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

list comprehension doesn't work with input [python]

I am trying to make a plan that allows you to insert strings to a list until you type a certain keyword which then locks the list from further appending.

print("I will list everything you desire!")
list = []
while(input("")!="stop"):
    shop_list = [list.append(i) for i in input("")]
print(shop_list)
list.clear()

yet once I run this program I get an output, that has a few issues.
output:
[None,None] (amount of None is per the number of inputs you give)

In addition to this, after the program finishes running I don’t seem to get list cleared. I don’t understand why, considering that the clear function should do just as I intend.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

regarding the first problem, I assume it is due to incorrect use of input("") in the extent of list comprehension.

and with the latter issue, I suppose I might have used clear function incorrectly. although this is how I should use it.

>Solution :

You prob. can try this, as earlier comment pointed out your syntax is off… so it needs to be rewrite and corrected:


print("I will list everything you desire!")
# L = []
shop_list = []

while True:                    # Loop continuously
    item = input()             # get the input - one item at a time (one line) 
    if item != 'stop':         #  not done yet....
        shop_list.append(item)
    else:
        break
    
print(f' the shop list: {shop_list}')

shop_list.clear()

# Outputs Example:  assuming typing each word by itself (one line each word)
# the shop list: ['apple ', 'orange', 'banana ']
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading