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 convert them one by one in a for-loop and join them with ,:
print(','.join(str(x) for x in list_of_ints))
Answer from Mark Byers on Stack OverflowCalling 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 convert them one by one in a for-loop and join them with ,:
print(','.join(str(x) for x in list_of_ints))
There's nothing wrong with passing integers to str. One reason you might not do this is that myList is really supposed to be a list of integers e.g. it would be reasonable to sum the values in the list. In that case, do not pass your ints to str before appending them to myList. If you end up not converting to strings before appending, you can construct one big string by doing something like
', '.join(map(str, myList))
Coverting all the list elements to string using Python? - Stack Overflow
python - Turn each element of a list into a separate string? - Stack Overflow
python - Convert a list of elements in a string - Stack Overflow
Converting a lists of characters into a list of strings
That wouldn't work, since that would convert the entire list into one string:
roll = [1, 2, 3]
roll = str(roll)
print(roll)
# Prints [1, 2, 3]
print(type(roll))
# Prints <class 'str'>
You can instead use a list comprehension, to convert each item in the list one by one:
roll = [1, 2, 3]
roll = [str(r) for r in roll]
print(roll)
# Prints ['1', '2', '3']
print(type(roll))
# Prints <class 'list'>
Thinking in terms of types, the original variable you have is of type List[int] ([1, 2, 3]).
When using the str function, the result is an element of type str (aka '[1, 2, 3]'), but that's not the result that you want since you want to convert each individual element in the list to str, which makes the desired type to be List[str] (['1', '2', '3']).
In python, there are several canonical ways to apply a function to each individual element:
List Comprehension
new_roll = [str(x) for x in roll] # ['1', '2', '3']
This method is the simplest. It is basically a one-liner that does the followings:
new_roll = []
for x in roll:
new_roll.append(str(x))
Notice that the original variable roll won't be mutated since it is creating a new list.
Map
Another way to do the same thing is by using the map function in python. However for efficiency reason, the type of the output is of type Map[str] instead of type List[str].
roll_map = map(str, roll)
for x in roll_map:
print(x)
It is basically doing the followings:
roll = [1, 2, 3]
for x in roll:
str_x = str(x)
print(str_x)
Notice that no new list has been created in order to save memory. This method is generally applicable when you need the result of a function applied to a list (or an iterator in general) but you do not want to store the results of the function application (to save memory).
A common hack to obtain a list via the map function is by list(map(str, roll)).
This should work:
for e in map(str, myList):
#do your stuff here, e is '[3, 95]' on the fst element and so on
map applies a function to each element in myList. Using the str function will transform each element in your list in a string so you can use it freely.
mylist = [[3, 95],[8, 92],[18, 25],[75, 78],[71, 84],-9999,[96, 50],[91, 70],-9999,[19, 60]]
# convert all elements
new_list = [str(e) for e in mylist ]
# use it
for str1 in new_list:
print str1
or
mylist = [[3, 95],[8, 92],[18, 25],[75, 78],[71, 84],-9999,[96, 50],[91, 70],-9999,[19, 60]]
for x in mylist:
# convert one element and use it
str1 = str(x)
print str1
Let the str.join() handle that:
>>> a = ['Maria','Luis','Andrea']
>>> ", ".join(a)
'Maria, Luis, Andrea'
That's what the str.join method is for:
>>> ', '.join(a)
'Maria, Luis, Andrea
The method concatinates all the elements from an iterable with the string join is being called on as the separator.
Some examples:
>>> 'SEP'.join(a)
'MariaSEPLuisSEPAndrea'
>>> ''.join(a)
'MariaLuisAndrea'
It can be seen as the counterpart of str.split:
>>> s = ', '.join(a)
>>> s
'Maria, Luis, Andrea'
>>> s.split(', ')
['Maria', 'Luis', 'Andrea']