0

I am trying t convert a number as follows:

input -> 123456 output -> ['1','2','3','4','5','6']

The following loop does work:

number = 123456
defg = []
abc = str(number)
for i in range(len(abc)):
    defg.append(abc[i])

However, when i try this in the form of a one line for loop, the output is ['None','None','None','None']

My one line loop is as follows:

number = 123456
defg = []
abc = str(number)
defg= [[].append(abc[i]) for i in range(len(abc))]

Any idea what I am doing wrong?

4 Answers4

0

here the solution

list(map(lambda x: str(x), str(number)))       
Out[13]: ['1', '2', '3', '4', '5', '6']

or you can do this

list(map(str, str(number)))                                                                                                                                                         
Out[13]: ['1', '2', '3', '4', '5', '6']

or this

list(str(number))
['1', '2', '3', '4', '5', '6']

duplicate you can see more here Splitting integer in Python?

Beny Gj
  • 607
  • 4
  • 16
0

Try this:

x = 527876324

print(list(str(x))

Output

['5', '2', '7', '8', '7', '6', '3', '2', '4']
Dhaval Taunk
  • 1,662
  • 1
  • 9
  • 17
0

The answers above are concise, but I personally prefer vanilla list comprehension over map, as powerful as it is. I add this answer simply for the sake of adding diversity to the mix, showing that the same can be achieved without using map.

str_list = [abc[i] for i in range(len(abc))]

We can strive for a more Pythonic solution by avoiding direct indexing and using a simpler loop.

better_str_list = [char for char in abc]

This list comprehension is basically just a translation of the for loop you already have.

Jake Tae
  • 1,681
  • 1
  • 8
  • 11
  • Thanks! This works well, but i was really hoping to understand what was going wrong with the one liner that i had written. – mobiustrip Apr 26 '20 at 06:30
  • I have gained the insight i needed from your edited response. Thank you! – mobiustrip Apr 26 '20 at 06:38
  • Glad it helped! I was just about to say, think of it this way: by appending an element in the original for loop, you're adding that element to the list. When we use list comprehension, we can directly reference what object we want to 'append' by simply stating it in the one-liner. – Jake Tae Apr 26 '20 at 06:40
0

Solution

As your expection the result is

number = 123456
defg = []
abc = str(number)
[defg.append(abc[i]) for i in range(len(abc))]
print(defg)
When you run the loop the values are append to defg but it return none. you assign the None value to defg so it will show the None output

Recomanded way

You can use list metnod to convert the number to list

number = 865393410
result = list(str(number))
print(result)

If you want int in list try this

number = 865393410
result = []
for i in list(str(number)):
    result.append(int(i))

print(result)

With single line loop

number = 865393410
result = []
[result.append(int(i)) for i in list(str(number))]
print(result)

The last one is recommended for you

JRudransh
  • 77
  • 13