Skip to content
Advertisement

Python: How to add specific character as transforming a list to a string in differentiating the last item in the list?

I want to transform a list to a string including the square brackets and comma and then concatenate with another string.

JavaScript

What I’m trying is like this:

JavaScript

But this results in the following output:

JavaScript

How to prevent the last comma ‘,’ from generating as the process of transform?

I’m considering to take a specific process based on whether the item is the last one in the list.

But how can I know it?

Or else is there any other solution?

Really appreciate!

Advertisement

Answer

This is bad Approah:

JavaScript

A good Approach is using f-Strings.

JavaScript

if u doesn’t like f-strings then

JavaScript
Advertisement