How to Convert Each Item in the List to String, for the Purpose of Joining Them

How can I convert each item in the list to string, for the purpose of joining them?

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))

Converting list values into a string facing an error in python using .join()

def dec_to_bin(num):
bin_num = []

while num > 1:
bin_num.append(num % 2)
num = num // 2

if num == 1:
bin_num.append(1)

return(bin_num[::-1])

sample = dec_to_bin(19)
ans = ("".join(str(x) for x in sample))
print(ans)

Python - joining item in list with strings

This is a simple Python str.join operation. The problem that I just realized was that you have to convert the integers in the list to strings first. You can do that with a simple list comprehension like this.

'x'.join([str(x) for x in my_list])

Java: convert List String to a join()d String

String.join

With Java 8 you can do this without any third party library.

If you want to join a Collection of Strings you can use the String.join() method:

List<String> list = Arrays.asList("foo", "bar", "baz");
String joined = String.join(" and ", list); // "foo and bar and baz"

Collectors.joining

If you have a Collection with another type than String you can use the Stream API with the joining Collector:

List<Person> list = Arrays.asList(
new Person("John", "Smith"),
new Person("Anna", "Martinez"),
new Person("Paul", "Watson ")
);

String joinedFirstNames = list.stream()
.map(Person::getFirstName)
.collect(Collectors.joining(", ")); // "John, Anna, Paul"

The StringJoiner class may also be useful.

convert list to string in python

Join doesn’t work the way you think it works.

What join does:

",".join(["a", "b", "c"])

Gives "a,b,c". Essentially it creates a string by elements from a list with what you provided before .join, in this case it’s a comma.

So what you want can be achieved by

",".join(str(x) for x in l)

The inside expression changes the integers in list l into strings before joining them by comma.

Convert list to string using python

Firstly convert integers to string using strusing map function then use join function-

>>> ','.join(map(str,[10,"test",10.5]) )#since added comma inside the single quote output will be comma(,) separated
>>> '10,test,10.5'

Or if you want to convert each element of list into string then try-

>>> map(str,[10,"test",10.5])
>>> ['10', 'test', '10.5']

Or use itertools for memory efficiency(large data)

>>>from itertools import imap
>>>[i for i in imap(str,[10,"test",10.5])]
>>>['10', 'test', '10.5']

Or simply use list comprehension

>>>my_list=['10', 'test', 10.5]
>>>my_string_list=[str(i) for i in my_list]
>>>my_string_list
>>>['10', 'test', '10.5']

covert list of integers to string

You need to convert each item to string first
''.join(str(x) for x in list)

How to concatenate (join) items in a list to a single string

Use str.join:

>>> words = ['this', 'is', 'a', 'sentence']
>>> '-'.join(words)
'this-is-a-sentence'
>>> ' '.join(words)
'this is a sentence'


Related Topics



Leave a reply



Submit