8

I have the list it contain int ,float and string:

lists = [10, "test", 10.5]

How Can i convert above list to string? I have tried:

val = ','.join(lists)
print val

I am getting error like this:

sequence item 0: expected string, int found

How can I solve this issue?

Santosh Kumar
28.3k21 gold badges72 silver badges126 bronze badges
asked Nov 10, 2015 at 12:11
1

10 Answers 10

18

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']

EDIT For Python 3

Python 3 does not have imap(). Built-in map works as same as imap.

answered Nov 10, 2015 at 12:12
Sign up to request clarification or add additional context in comments.

1 Comment

imap no longer included in itertools using Python 3. Use map instead. stackoverflow.com/questions/30271712/…
6

The easiest way is to send the whole thing to str() or repr():

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

repr() may produce a different result from str() depending on what's defined for each type of object in the list. The point of repr() is that you can send such strings back to eval() or ast.literal_eval() to get the original object back:

>>> import ast
>>> lists = [10, "test", 10.5]
>>> ast.literal_eval(repr(lists))
[10, 'test', 10.5]
answered Nov 10, 2015 at 12:53

Comments

4
a = ['b','c','d']
strng = ''
for i in a:
 strng +=str(i)
print strng
Stephen Rauch
50.1k32 gold badges119 silver badges143 bronze badges
answered Dec 20, 2017 at 18:59

1 Comment

a = ['b','c','d'] ==> Let this be a list strng = '' ==> Lets initialise a variable strng with empty string " for i in a: ==> Interate over each element of list and concatenate with strng strng +=str(i) print strng ==> Finally print the required strng
1

The error you are getting because join wants elements to be string type, but in your list there is integer too, so 1st you have to convert them to type string.

you can use list comprehension and str and join to join them

>>> lists = [10,"test",10.5]
>>> ",".join(str(x) for x in lists)
answered Nov 10, 2015 at 12:18

Comments

1

You have to pass each item in your list as a string into the ','.join(sequence). Consider using:

val = ','.join([str(item) for item in lists])

print val

answered Nov 10, 2015 at 12:43

1 Comment

Simplest solution, using a list comprehension and taking advantage that str(item) doesn't change item if it is already a str. This or TigerhawkT3 solution should be the selected answer.
0

If you want to convert each element in the list to a string, you could do it simply using a for-loop.

for i in range(len(lists)):
 lists[i] = str(lists[i])

Alternatively, if you want to make one string where all elements are joined together, you could edit the code above slightly.

string_of_lists = ""
for i in lists:
 string_of_lists += str(i)

As you can tell, this is another way of doing it, apart from the other solutions using join.

I hope I helped!

answered Nov 10, 2015 at 12:21

Comments

0

This is also possible. Here x variable is list.

>>> '%s'*len(x) % tuple(x)
answered Sep 5, 2017 at 5:19

Comments

0

As mentioned here

 list=['a/b/c', 'd/e/f']
 file_list_string= ' '.join(list)
 file_list_string= ' '.join(str(file) for file in list)
answered Dec 4, 2018 at 8:56

Comments

0
import functools
lists = [10,"test",10.5]
print(functools.reduce(lambda x,y:x+","+y,list(map(str,lists))))
Paul Roub
36.5k27 gold badges88 silver badges95 bronze badges
answered Jun 20, 2019 at 13:25

2 Comments

Can you explain a bit more?
in the reduce function initially x and y take the value from list and after that x store the resultant values and y gets the new value from list. (eg. x=10 and y= test then x+","+y = 10,test and now in second step x= 10,test and y=10.5 now x+","+y) become 10,test,10.5 resultant answer)
0

You could always do it the dirty way:

 list_name = ["a", "b", "c"];
 string_name = "";
 for c in list_name:
 string_name += c
 print(string_name)
 

OUTPUT:

 "abc"

That should work with ints, floats, and strings, always converting them to string type.

answered Jul 20, 2020 at 16:59

1 Comment

Welcome to SO! Thank you for your time answering this question. However, please try your solution with the list provided by the OP. Maybe you want to change your solution aftwerwards.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.