Python Forum

Full Version: Printing List in one line
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Am new to coding. i write a program to print output in a list
 numb = [1, 2, 3 , 4 , 5 , 6 , 7, 22 ]
print ([a for a in numb if a < 7])
this gives me below error
Error:
File "C:/Users/ASUS/.PyCharmCE2019.1/config/scratches/Pratice.py", line 2 print set([a for a in numb if a < 7]) ^ SyntaxError: invalid syntax
***************************************************************************************

i tried this as well to print in a list
numb = [1, 2, 3 , 4 , 5 , 6 , 7, 22]
for a in numb:
    if a < 7:
        print(a, sep=', ' )
This give me output as
Output:
1 2 3..
but not as 1, 2,3 ...

Thanks
Bharat
I would do something like this
aList = [1, 2, 3, 4, 5, 6, 7]
newList = str(aList)
output = ''
for character in newList:
    if character != '[' and character != ']':
        output += character
print(output)
In the first part, the code in the error is not the code you showed. Both print and set are functions, they both need parentheses around them to contain their parameters. It looks like you added set using print's parens, you need to add another pair around the set call.

In the second part, the sep parameter is put between items printed in that call of print. It doesn't carry over to the next call of print. The end parameter would do what you want, but it would leave an extra comma at the end of the list. Typically you would use join in that situation: print(', '.join([a for a in numb if a < 7])).
The error shown does not match with the code shown () are missing from the print
print([n for n in aList if n < 7])
(May-24-2019, 10:19 PM)SheeppOSU Wrote: [ -> ]I would do something like this
aList = [1, 2, 3, 4, 5, 6, 7]
newList = str(aList)
output = ''
for character in newList:
    if character != '[' and character != ']':
        output += character
print(output)

i want a output which simply print output in one line as a list. i got something else after using your suggested code
Output:
1 1, 1, 1, 2 1, 2, 1, 2, 1, 2, 3 1, 2, 3, 1, 2, 3, 1, 2, 3, 4 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 5 1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 6 1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6, 7 1, 2, 3, 4, 5, 6, 7, 1, 2, 3, 4, 5, 6, 7, 1, 2, 3, 4, 5, 6, 7, 2 1, 2, 3, 4, 5, 6, 7, 22 1, 2, 3, 4, 5, 6, 7, 22]

(May-24-2019, 10:22 PM)ichabod801 Wrote: [ -> ]In the first part, the code in the error is not the code you showed. Both print and set are functions, they both need parentheses around them to contain their parameters. It looks like you added set using print's parens, you need to add another pair around the set call. In the second part, the sep parameter is put between items printed in that call of print. It doesn't carry over to the next call of print. The end parameter would do what you want, but it would leave an extra comma at the end of the list. Typically you would use join in that situation: print(', '.join([a for a in numb if a < 7])).
thanks, that worked for me :)
One possibility is to combine unpacking with sep:

>>> numb = [1, 2, 3 , 4 , 5 , 6 , 7, 22 ]
>>> print(*(num for num in numb if num <7), sep=",")
1,2,3,4,5,6