Python Forum
Help with List Loops - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Help with List Loops (/thread-22732.html)



Help with List Loops - slackerman73 - Nov-24-2019

I'm working through an assignment and am having some difficulty. Code below

listToPrint=['dog', 'cat', 'hamster', 'pig']

while len(listToPrint) > 1:
print ('Your list consists of ' + str(listToPrint[0:-1]) + ' and ' + str(listToPrint[-1]) +'.')
break
else:
print ('Your lists consists of ' + str(listToPrint[0]) + '.')

Output is supposed to be:
Your list consists of dog, cat, hamster, and pig. #Note the commas and the and in the sentence.

Instead I'm getting:
Your list consists of ['dog', 'cat', 'hamster'] and pig.

I can't figure out two things. How do i get rid of the brackets and add the commas. Any help is appreciated.

I'm working through an assignment and am having some difficulty. Code below
listToPrint=['dog', 'cat', 'hamster', 'pig']

while len(listToPrint) > 1:
     print ('Your list consists of ' + str(listToPrint[0:-1]) + ' and ' + str(listToPrint[-1]) +'.')
     break
else:
     print ('Your lists consists of ' + str(listToPrint[0]) + '.')
Output is supposed to be:
Your list consists of dog, cat, hamster, and pig. #Note the commas and the and in the sentence.

Instead I'm getting:
Your list consists of ['dog', 'cat', 'hamster'] and pig.

I can't figure out two things. How do i get rid of the brackets and add the commas. Any help is appreciated.


RE: Help with List Loops - ichabod801 - Nov-24-2019

Try print(', '.join(list_to_print)).


RE: Help with List Loops - slackerman73 - Nov-24-2019

Thank you Craig....and thanks for making me work for it.

You gave me the code snippet but I still had to manipulate it a little get it the way I needed.

listToPrint = []
while True:
    newWord = input("Enter a word to add to the list (press return to stop adding words) > ")
    if newWord == "":
        break
    else:
        listToPrint.append(newWord)

while len(listToPrint) > 1:
    print ('Your list consists of ' + (', '.join(listToPrint[0:-1]) + ', and ' + str(listToPrint[-1]) +'.'))
    break
else:
    print ('Your lists consists of ' + str(listToPrint[0]) + '.')



RE: Help with List Loops - erfanakbari1 - Nov-24-2019

What I recommend you and you need to do is to add some for loops in your while loop . in order to get indivisual items in your list .
this code below can probably fix your program , but that's not the only way .
listToPrint = ['dog', 'cat', 'hamster', 'pig']

while len(listToPrint) > 1:
    print("Your list consists of : ")
    for i in listToPrint:
        print(i)
    break
else:
    print('Your lists consists of ' + str(listToPrint[0]) + '.')
If you are uncomfortable using this method , I can also show you some diffrent ways of doing it .


RE: Help with List Loops - perfringo - Nov-25-2019

ichabod801 gave very strong hint how to achieve desired result.

>>> lst = ['dog', 'cat', 'hamster', 'pig']
>>> f'Your list consists {", ".join(lst[:-1])}, and {lst[-1]}.'
'Your list consists dog, cat, hamster, and pig.'