0

I have an integer object for example a = 1234 and I want to convert it to a list, so it would look something like [1234].

I tried converting it into a string first and then converting it to a list but that gives me [1,2,3,4]. Any suggestions?

2
  • 1
    Your title and your question text are completely different. Do you want a list or a string? Commented May 27, 2021 at 1:08
  • Why do you need to convert it to a string first? Just [a] will make a list. Commented May 27, 2021 at 1:08

5 Answers 5

2

You can just cover it in brackets.

a = 1234
print([a])

Or append()

b = []
b.append(a)

output

[1234]
Sign up to request clarification or add additional context in comments.

Comments

0

Here:

a = 1234
lst = []
lst.append(a)

print(lst) #[1234]

Comments

0

Just cover it with brackets:

a=1243
a=[a]

or create a list and append a

b = []
b.append(a) # or you could do b=[a]

Comments

0

If you are trying to put your int in a list, use []:

>>> var = 1234
>>> [var]
[1234]

or do:

>>> l = []
>>> l.append(var)
>>> l
[1234]

Comments

-1

Well it depends. If you need the int to be a single member of a list then [a] works. If you need each digit in the integer to be an individual member of the list then list(str(a)) will work for you.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.