J.doe
J.doe

Reputation: 67

How to remove the last comma in a list of integers?

The list is supposed to get the square of each number. I've managed to do that but I need to remove the last comma in the sequence.

When I use this code:

def multiplicator():
      for a in range(3, 20):
          b = (a*a)
          print(b, end=",")

multiplicator()

I get:

9,16,25,36,49,64,81,100,121,144,169,196,225,256,289,324,361,

Upvotes: 1

Views: 6872

Answers (6)

mohsen
mohsen

Reputation: 181

def multiplicator():
    print_list = list()
    for a in range(3, 20):
        b = (a*a)
        print_list.append(b)
        print_list.append(',')
    for i in print_list[:-1] :
      print(i, end='')

multiplicator()

OUTPUT :

 9,16,25,36,49,64,81,100,121,144,169,196,225,256,289,324,361

Upvotes: 1

agastya
agastya

Reputation: 376

A one line way of doing it:

a = range(3, 20)
print(*[i**2 for i in a], sep=',')

The list is created using list comprehension, then the unpacked list (using the * operator) is printed; with ',' being the separator.

Output will be:

9,16,25,36,49,64,81,100,121,144,169,196,225,256,289,324,361

Upvotes: 0

Ashim Bhargav
Ashim Bhargav

Reputation: 11

def funcPattern(n):

# Base case (When n becomes 0 or negative)
if (n == 0 or n < 0):
    print(n, end=", ")
    return

print(n, end=", ")

# First print decreasing order
funcPattern(n - k)
if (n == m):
    print(n, end=" ")
elif (n != m):
    print(n, end=", ")

n = 10 m = n k = 2 funcPattern(n)

Upvotes: 0

ettanany
ettanany

Reputation: 19806

A simple option would be:

def multiplicator():
    for a in range(3, 19):
        print(a*a, end=',')  # directly a*a, no need for an intermediate variable
    print(19*19)

A generic solution would be:

def multiplicator(n):
    for a in range(3, n-1):
        print(a*a, end=',')
    print((n-1)*(n-1))

Output:

>>> multiplicator(20)
9,16,25,36,49,64,81,100,121,144,169,196,225,256,289,324,361

Note:

This is a simple and similar approach to what you are trying to do, but you should definitely go for @CoryKramer's answer using str.join()

Upvotes: 0

Djaouad
Djaouad

Reputation: 22776

You can keep your loop and add a condition:

def multiplicator():
    for a in range(3, 20):
        b = (a*a)
        print(b, end="")
        if a<19: # if not the last element
          print(end=",") # print ","
    print() # print new line after everything

multiplicator() # => 9,16,25,36,49,64,81,100,121,144,169,196,225,256,289,324,361

You can also use a ternary condition to shorten the code:

def multiplicator():
    for a in range(3, 20):
        b = (a*a)
        print(b, end="," if a<19 else "")

    print() # print new line after everything
multiplicator() # => 9,16,25,36,49,64,81,100,121,144,169,196,225,256,289,324,361

Upvotes: 1

Cory Kramer
Cory Kramer

Reputation: 117856

You can use str.join to add a delimiter between strings which will handle not adding an extra one to the end.

>>> ','.join(str(a*a) for a in range(3, 20))
'9,16,25,36,49,64,81,100,121,144,169,196,225,256,289,324,361'

Upvotes: 6

Related Questions