Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Printing List in one line
#1
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
Reply
#2
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)
Reply
#3
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])).
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#4
The error shown does not match with the code shown () are missing from the print
Reply
#5
print([n for n in aList if n < 7])
Reply
#6
(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 :)
Reply
#7
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
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Question Printing through list.. again jesse68 2 1,140 Apr-16-2022, 03:24 PM
Last Post: jesse68
  help for list printing jip31 8 3,616 May-01-2021, 03:52 AM
Last Post: Pedroski55
  Printing a specific line from a JSON serpiente 4 3,778 Mar-14-2021, 07:27 PM
Last Post: buran
  Problem printing last element from a list tester_V 3 2,374 Oct-30-2020, 04:54 AM
Last Post: tester_V
  Printing empty list? hhydration 2 2,109 Oct-28-2020, 11:34 AM
Last Post: Atekka
  Passing List of Objects in Command Line Python usman 7 3,157 Sep-27-2020, 03:45 PM
Last Post: ndc85430
  How to create and define in one line a 2D list of class objects in Python T2ioTD 1 2,030 Aug-14-2020, 12:37 PM
Last Post: Yoriz
  Printing images from a list Heyjoe 4 2,811 Jun-22-2020, 02:28 AM
Last Post: Heyjoe
  item from a line to list however when i print the line instead of words i get letters Sutsro 5 2,946 Apr-22-2020, 02:39 PM
Last Post: deanhystad
  printing a list contents without brackets in a print statement paracelx 1 2,115 Feb-15-2020, 02:15 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020