-5

convert a binary string into list in python
example '10011' is to be converted as list ['1','0','0','1','1'] ?

Nikhil Lingam
  • 121
  • 2
  • 12

1 Answers1

0

There is a function called list to do this directly.This can convert any string into list of characters.

list('01101')

will return

['0', '1', '1', '0', '1']

One more way is

a = '01101'
a_list=[]
for item in a:
    a_list.append(item)
print(a_list)
  • For the second example, why do you need `i`? `for item in a: list.append(item)` would be fine. Also naming a list `list` just shadows the builtin function [`list()`](https://docs.python.org/3/library/functions.html#func-list), which is never a good idea. – RoadRunner Dec 06 '18 at 07:16
  • 1
    @RoadRunner just wondering if `list()` is considered a function. The documentation says *"Rather than being a function, list is actually a mutable sequence type"*, but also includes it in [Built-in functions list](https://docs.python.org/3/library/functions.html) – Aykhan Hagverdili Dec 06 '18 at 09:06
  • @Ayxan I guess it can be looked that way. This might be a good question to ask honestly, I'm curious to see what others have to say. – RoadRunner Dec 06 '18 at 10:51
  • @RoadRunner asked it. Here is the [question](https://stackoverflow.com/q/53649958/10147399) – Aykhan Hagverdili Dec 06 '18 at 11:05
  • @RoadRunner I didn't give much time to it yesterday.. I taken your inputs.Thank you – Yoshitha Penaganti Dec 07 '18 at 09:58