0

What is the easiest way to 'split' a list into a string in Python?

To create a list from a string:

s='1\t2\t3\t4'

we use:

s.split('\t',-1)

I tried using the split function to a list:

l=[1,2,3,4]

to get a string:

s='1\t2\t3\t4'

but I got an AttributeError telling that list has no attribute split.

teaLeef
  • 1,879
  • 2
  • 16
  • 26

3 Answers3

9

str.split is a method that is only available on the str type. How it works is explained in the docs:

str.split([sep[, maxsplit]])

Return a list of the words in the string, using sep as the delimiter string. If maxsplit is given, at most maxsplit splits are done (thus, the list will have at most maxsplit+1 elements). If maxsplit is not specified or -1, then there is no limit on the number of splits (all possible splits are made).


What you want to use here is str.join:

>>> l = [1, 2, 3, 4]
>>> map(str, l) # Just to demonstrate
['1', '2', '3', '4']
>>> '\t'.join(map(str, l))
'1\t2\t3\t4'
>>>

According to the docs:

str.join(iterable)

Return a string which is the concatenation of the strings in the iterable iterable. The separator between elements is the string providing this method.

In the above example, str.join takes the list of strings returned by map(str, l):

['1', '2', '3', '4']

and concatenates all of its items together, separating each one by \t. The result is the string:

'1\t2\t3\t4'

Also, in case you are wondering, map is being used in the above example to convert the list of integers into a list of strings. An alternate approach would be to use a list comprehension:

>>> l = [1, 2, 3, 4]
>>> '\t'.join([str(x) for x in l])
'1\t2\t3\t4'
>>>

Both solutions are about equivalent, but you need to use one of them because str.join requires an iterable of strings.

  • 1
    Simply using a generator is actually enough here :) – Samy Arous Mar 19 '14 at 18:09
  • 1
    @SamyArous - Yes, but it is generally less efficient. See [here](http://stackoverflow.com/a/9061024/2555451). –  Mar 19 '14 at 18:11
  • +1 for the inclusion of map and the list comp. A corollary on how to convert back to a list (using `str.split` as OP originally thought he had to do) would be further enlightening. – Adam Smith Mar 19 '14 at 18:11
  • 1
    @iCodez This is actually very interesting. Thank you for the link :) – Samy Arous Mar 19 '14 at 18:12
  • @adsmith - I'm sorry, but I do not know what you mean. If I am not mistaken, the OP already knows how to convert a string into a list. He was having trouble going the other way. –  Mar 19 '14 at 18:19
  • @iCodez It seems to me that OP is confused on how `split` works, since he's trying to do `list.split` to get a string. If I were you, I would add a short note explaining how `str.split` works and mention that it's a method belonging to a `str`, and how it differs from `str.join` in more explicit terms. – Adam Smith Mar 19 '14 at 18:21
1

You can do this.

l = [1, 2, 3, 4]
'\t'.join([str(x) for x in l])

join-Python docs

Anish Shah
  • 7,669
  • 8
  • 29
  • 40
0

That's not splitting, that's joining: '\t'.join(str(i) for i in l)

a p
  • 3,098
  • 2
  • 24
  • 46
  • 1
    Won't work, a `TypeError` will be raised as you cannot join together integers as they are not iterable. You need to explicitly convert the integers to strings. – anon582847382 Mar 19 '14 at 18:15