Posts: 3
Threads: 2
Joined: May 2019
May-24-2019, 10:06 PM
(This post was last modified: May-24-2019, 10:19 PM by Yoriz.)
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
Posts: 479
Threads: 86
Joined: Feb 2018
May-24-2019, 10:19 PM
(This post was last modified: May-24-2019, 10:19 PM by SheeppOSU.)
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)
Posts: 4,220
Threads: 97
Joined: Sep 2016
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])) .
Posts: 2,168
Threads: 35
Joined: Sep 2016
The error shown does not match with the code shown () are missing from the print
Posts: 12,024
Threads: 484
Joined: Sep 2016
print([n for n in aList if n < 7])
Posts: 3
Threads: 2
Joined: May 2019
May-26-2019, 08:13 PM
(This post was last modified: May-26-2019, 08:23 PM by bharat_s579.)
(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 :)
Posts: 1,950
Threads: 8
Joined: Jun 2018
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.
|