Avoid Printing the Last Comma

How to remove last comma from print(string, end=“, ”)

You could build a list of strings in your for loop and print afterword using join:

strings = []

for ...:
# some work to generate string
strings.append(sting)

print(', '.join(strings))

alternatively, if your something has a well-defined length (i.e you can len(something)), you can select the string terminator differently in the end case:

for i, x in enumerate(something):
#some operation to generate string

if i < len(something) - 1:
print(string, end=', ')
else:
print(string)

UPDATE based on real example code:

Taking this piece of your code:

value = input("")
string = ""
for unit_value in value.split(", "):
if unit_value.split(' ', 1)[0] == "negative":
neg_value = unit_value.split(' ', 1)[1]
string = "-" + str(challenge1(neg_value.lower()))
else:
string = str(challenge1(unit_value.lower()))

print(string, end=", ")

and following the first suggestion above, I get:

value = input("")
string = ""
strings = []
for unit_value in value.split(", "):
if unit_value.split(' ', 1)[0] == "negative":
neg_value = unit_value.split(' ', 1)[1]
string = "-" + str(challenge1(neg_value.lower()))
else:
string = str(challenge1(unit_value.lower()))

strings.append(string)

print(', '.join(strings))

omitting last comma when printing a list

Answering the question but not accounting for the point raised by @Bathsheeba:

#include <stdio.h>

int main()
{
int n1,n2,f,i,j;

scanf("%d %d", &n1, &n2);

int itemCount = 0; // NEW

for(i=n1; i<n2; ++i)
{
f=0;
for(j=2; j<=i/2; ++j)
{
if(i%j==0)
{
f=1;
break;
}
}

if (f == 0 && i != 1) // TESTS COMBINED
{
if (itemCount++ > 0) putchar(','); // HERE
printf("%d",i); // COMMA REMOVED
}
}
printf("\n"); // newline at the end
return 0;
}

The only way to really remove a comma after you've added it is if you're building a buffer at runtime - which is a reasonable approach - so here we have to only generate a comma when we need it, before all but the first item.

How to avoid last comma in python loop

Pass the results as separate arguments and specify the separator:

 print(*result, sep=',')

How to not print the last comma of a list of integers?

One of the solution is to use boolean flag:

bool first = true;
for( ... ) {
if( first ) first = false;
else std::cout << ',';
std::cout << data;
}

How to stop printing comma and space after last digit in c

There are many ways to solve your problem. You can print a separator before each of the outputs except the first one.

Note also that it would be much more readable to use '0' and '9' instead of hard coding ASCII values such as 48...

Here is a modified version:

#include <stdio.h>

int main() {
int d1, d2, n = 0;

for (d1 = '0'; d1 <= '9'; d1++) {
for (d2 = d1 + 1; d2 <= '9'; d2++, n++) {
if (n > 0) {
putchar(',');
putchar(' ');
}
putchar(d1);
putchar(d2);
}
}
putchar('\n');
return 0;
}

Here is an alternative using a separator string:

#include <stdio.h>

int main() {
int d1, d2;
const char *sep = "";

for (d1 = '0'; d1 <= '9'; d1++) {
for (d2 = d1 + 1; d2 <= '9'; d2++) {
fputs(sep, stdout);
sep = ", ";
putchar(d1);
putchar(d2);
}
}
putchar('\n');
return 0;
}

C++ Remove last comma when printing AVL tree elements

Make an internal implementation function that maintains a flag that indicates whether the data item to be output is the first one. Then prepend the output with a comma for all items but the first.

void inOrderImpl(Node* root, bool& first)
{
if(root != nullptr) {
inOrderImpl(root->left, first);
if (first)
first = false;
else
cout << ", ";
cout << root->data;
inOrderImpl(root->right, first);
}
}

void inOrder(Node* root)
{
bool first = true;
inOrderImpl(root, first);
}


Related Topics



Leave a reply



Submit