a = [b'food']
b= list(b'food')
Output
a = [b'food']; b = [102, 111, 111, 100]
Patrick Haugh
61.4k13 gold badges94 silver badges101 bronze badges
3 Answers 3
list(...) takes an iterable as parameter. b'food' is of type bytes,
and so list(...) creates a list from the bytes in it (b'f', b'o', ...).
The equivalent of [b'food'] using list(...) would be:
b = list((b'food',))
answered Oct 16, 2017 at 5:08
janos
126k31 gold badges243 silver badges253 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
Because list() take iterable items as parameters and unpack them.
list("123") => ["1", "2", "3"]
answered Oct 16, 2017 at 5:07
Sraw
20.6k11 gold badges61 silver badges93 bronze badges
Comments
The first one creates a list with one element (b'food').
Meanwhile the list function will convert a given iterable object to a list with a copy of its elements. So it creates a list with the binary representation of each letter of 'food'.
answered Oct 16, 2017 at 5:10
berna1111
1,8611 gold badge18 silver badges24 bronze badges
Comments
lang-py
[b'food']is a list containing a single element, which is abytesobject, whilelist(b'food')takes an iterable and makes a list out of it. Since abytesobject is an iterable,listmakes a list of the elements in the iterable.