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
  • 26,475
  • 20
  • 67
  • 118
Abdul Razak
  • 2,654
  • 2
  • 18
  • 24
  • Does this answer your question? [Concatenate item in list to strings](https://stackoverflow.com/questions/12453580/concatenate-item-in-list-to-strings) – bad_coder Jan 04 '20 at 17:54

10 Answers10

17

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']
Learner
  • 5,192
  • 1
  • 24
  • 36
  • `imap` no longer included in `itertools` using Python 3. Use `map` instead. https://stackoverflow.com/questions/30271712/i-cant-find-imap-in-itertools-in-python – SeaDude Nov 15 '21 at 06:43
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]
TigerhawkT3
  • 48,464
  • 6
  • 60
  • 97
4
a = ['b','c','d']
strng = ''
for i in a:
   strng +=str(i)
print strng
Stephen Rauch
  • 47,830
  • 31
  • 106
  • 135
Ashok
  • 41
  • 7
  • 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 – Ashok Dec 21 '17 at 11:55
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)
Hackaholic
  • 19,069
  • 5
  • 54
  • 72
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

COCO
  • 148
  • 1
  • 2
  • 14
  • 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. – mins Aug 20 '17 at 15:46
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!

Martijn Luyckx
  • 402
  • 2
  • 12
0

This is also possible. Here x variable is list.

>>> '%s'*len(x) % tuple(x)
Shameem
  • 2,664
  • 17
  • 21
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)
m.zohary
  • 34
  • 2
0
import functools
lists = [10,"test",10.5]
print(functools.reduce(lambda x,y:x+","+y,list(map(str,lists))))
Paul Roub
  • 36,322
  • 27
  • 84
  • 93
  • 4
    Can you explain a bit more? – Dieter Meemken Jun 20 '19 at 14:01
  • 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) – Prince Vijay Jun 22 '19 at 07:38
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.

  • 1
    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. – above_c_level Jul 20 '20 at 17:20