How to transform a list to string conditioned by the type of the list element

I have a list of strings which I want to transform to a single string.

The list contains integers which I don't want to separate them with space.

For example I have the list:

lst = ['This', 'house', 'has', 1, 2, 'rooms']

They way I am using to make a string is:

' '.join([str(elem) for elem in lst])

Which of course results in: 'This house has 1 2 rooms'

The intention is to get: 'This house has 12 rooms'

I am not sure how to condition the join statement from above based on the type of the element of the list in order to have the space if it is an a string and an empty string if it is an int.

Any suggestions ?



Read more here: https://stackoverflow.com/questions/66269369/how-to-transform-a-list-to-string-conditioned-by-the-type-of-the-list-element

Content Attribution

This content was originally published by wannabedatasth at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: