1
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
asked Oct 16, 2017 at 5:05
1
  • 1
    The distinction here is that [b'food'] is a list containing a single element, which is a bytes object, while list(b'food') takes an iterable and makes a list out of it. Since a bytes object is an iterable, list makes a list of the elements in the iterable. Commented Oct 16, 2017 at 5:11

3 Answers 3

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
Sign up to request clarification or add additional context in comments.

Comments

0

Because list() take iterable items as parameters and unpack them.

list("123") => ["1", "2", "3"]
answered Oct 16, 2017 at 5:07

Comments

0

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

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.