1

I'm sorry if this is a duplicate question, but I am new to Python and not quit sure how to ask for what I need. In the most basic sense, I would like to turn this:

a = ['10', '20', '30']

Edit:

It actually:

a = [10, 20, 30]

into

a = ['102030']

Thank you so much for your help!

Ian
  • 30,182
  • 19
  • 69
  • 107
Noah Sprenger
  • 61
  • 1
  • 1
  • 7

4 Answers4

2

How about,

a = ''.join(a)
a = [a]  # if you want to put the string into a list

same question here: How to collapse a list into a string in python?

Community
  • 1
  • 1
snowflake
  • 902
  • 1
  • 6
  • 18
1

The easiest will be using join with empty string:

a = ['10', '20', '30']
a = ''.join(a) #use a as result too

You will get:

'102030' #a

Edit:

Since your list is list of integer, you should "stringify" the integers first:

a = [10, 20, 30]
a = ''.join(str(x) for x in a)

or, if you want to put the single result as string to, then enclose the final result with [...]:

a = [''.join(str(x) for x in a)]
Ian
  • 30,182
  • 19
  • 69
  • 107
  • It doesn't seem to be working. The elements in my list don't have single quotes around each integer. Does this matter? – Noah Sprenger May 11 '17 at 01:11
  • 2
    It does matter. That means you have list of integer not list of string, is it? – Ian May 11 '17 at 01:12
  • Oh, how can I change it? – Noah Sprenger May 11 '17 at 02:13
  • Now I see! Thanks so much Ian! – Noah Sprenger May 11 '17 at 02:13
  • So how can I then make that string into a simple number value for a variable? Sorry for the lack of Python terms... – Noah Sprenger May 11 '17 at 02:27
  • @NoahSprenger in python it is very straight forward, just reverse it from using `str` to using `int`: `a = int(a)`; – Ian May 11 '17 at 02:29
  • I have one more question, and I think it should be the last... I need to select every other digit in a number and add them to a string or list ideally. I need to select every other number STARTING with the second (so 1 in python speak). Do you think you can help me? – Noah Sprenger May 11 '17 at 02:47
  • @NoahSprenger you could use python slicing to do so - it's incredibly simple to do this. You could look what it means in SO or you could post new question to elaborate your issue. :) – Ian May 11 '17 at 02:50
  • Ok. I think I will ask the question - sometimes I have trouble interpreting the meaning of other's questions. – Noah Sprenger May 11 '17 at 02:56
  • I asked the question. I would love your help if you want to look at the only other question under my account. – Noah Sprenger May 11 '17 at 03:03
  • @NoahSprenger I have seen it, I think you should post the question using what you currently have... that is, declaring the list and combining it like what is shown above, then you what is result you got and what you actually expect. Your current question has totally different issue since you have integer (instead of list) and you want to slice it which is unable to be done – Ian May 11 '17 at 03:04
  • @NoahSprenger answered... please take a look – Ian May 11 '17 at 03:10
0

Try this:

a = ['10', '20', '30']

print [''.join(a)]
Ajax1234
  • 69,937
  • 8
  • 61
  • 102
0

Late answer after edit:

[''.join(map(str, a))] # ['102030']
Jared Goguen
  • 8,772
  • 2
  • 18
  • 36