2

I have the list below, which contains strings and integers. How do I make a string of them? Tried to use

''.join(l)

but it does't work, because there are integers on the list(specifically, L[1] is an integer, and the others are all strings.). Can you help?

L=['1', 9, ':', '0', '5', ':', '4', '5']
#expected "19:05:45"
Paul Rooney
  • 20,879
  • 9
  • 40
  • 61
Breno Maia
  • 87
  • 9

4 Answers4

7

You can convert each number in the list into a string with a for loop or list comprehension. Like so:

l = ['1', 9, ':', '0', '5', ':', '4', '5']

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

'19:05:45'
boostedd
  • 268
  • 1
  • 3
  • 13
  • You can omit the square brackets in the second line. That looks cleaner. – Wombatz Oct 17 '19 at 23:16
  • @Wombatz be careful! Removing the square brackets would make that statement a generator expression instead of a list comprehension, the change isn't purely syntactic/visual. – AMC Oct 18 '19 at 01:56
  • @Alexander it surely isn't but the end result is the same plus it is more readable. – Wombatz Oct 18 '19 at 02:00
  • I don't, much to the contrary. I just commented on someone else's answer about how generator expressions might be _slower_ than list comprehensions when it comes to `join`. See: https://stackoverflow.com/a/9061024/11301900. – AMC Oct 18 '19 at 02:03
  • My comment was just to warn you that omitting the square brackets changes the meaning of the program, not just the look of it. – AMC Oct 18 '19 at 02:04
  • @AlexanderCécile join try to keep everything in memory then perform operation, that make it slow – sahasrara62 Oct 18 '19 at 02:10
  • @prashantrana You mean that’s what makes list comprehensions slower in `join`, right? – AMC Oct 18 '19 at 02:23
  • @AlexanderCécile yes – sahasrara62 Oct 18 '19 at 08:01
4

Generators are perfect in this case:

>>> l = ['1', 9, ':', '0', '5', ':', '4', '5']
>>> ''.join(str(x) for x in l)
'19:05:45'

This looks the same as a list comprehension but does not require the creation of another list instance.

Tim
  • 2,510
  • 1
  • 22
  • 26
  • It seems that in the case of `join`, a list comprehension may be better: https://stackoverflow.com/a/9061024/11301900. It certainly did not think that would be the case. – AMC Oct 18 '19 at 01:57
  • @AlexanderCécile You are correct there is a small performance improvement, however, in this case, the difference (on my machine) is +-0.2us. The list vs genex is quite complex for small sequences lists are often faster, most of my projects would quickly exhaust your machines ram without generators. – Tim Oct 18 '19 at 11:06
  • Oh yes, 99% of the time if you can use a generator expression then you should! – AMC Oct 18 '19 at 13:11
3

Just map all the elements to string before joining.

>>> ''.join(map(str,['1', 9, ':', '0', '5', ':', '4', '5']))
'19:05:45'
Paul Rooney
  • 20,879
  • 9
  • 40
  • 61
1

You can do it in a for loop

list_str = ''
for i in l:
    list_str += str(i)

Or a with a list comprehension

list_str = ''.join([str(i) for i in l])
Diego
  • 216
  • 1
  • 11