AKM AKM - 1 year ago 70
Python Question

Joining a list that has Integer values with Python

I need to join a list of items. Many of the items in the list are integer values returned from a function. ie:


How should I convert the returned result to a string in order to join it with the list?

Do I need to do the following for every integer value:


Is there a more Pythonic way to solve casting problems?

Answer Source

Calling str(...) is the Pythonic way to convert something to a string.

You might want to consider why you want a list of strings. You could instead keep it as a list of integers and only convert the integers to strings when you need to display them. For example, if you have a list of integers then you can do this:

print ', '.join(str(x) for x in list_of_ints)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download