1

How can I convert this:

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

into this

2314142332414132

I've tried using the .join method, but I'm quite new to Python and need some help using it. I've also tried working with re.sub, string.replace, but none of them worked. Any help?

user3870619
  • 55
  • 1
  • 7

2 Answers2

8

Try this:

values = ['2', '3', '1', '4', '1', '4', '2', '3', '3', '2', '4', '1', '4', '1', '3', '2']
value = ''.join(values)

If you want to have an int value, you can cast the resulting string to int:

value = int(value)
miindlek
  • 3,523
  • 14
  • 25
4
result = int(''.join(['1','2']))

See this question.

Community
  • 1
  • 1
  • Thanks, my error was about putting `''.join(my_list)`instead of `result =''.join(my_list)`. – user3870619 Aug 01 '14 at 10:21
  • This is very important conceptually. Python's variables don't have type, but its values do. You can never "transform" a value in-place in a way that changes its type; thus `''.join(my_list)` cannot possibly change `my_list` from being a list into being a string. Most built-in functions create new values rather than changing their input values, even if the returned thing is of the same type. – Karl Knechtel Aug 01 '14 at 10:54