[Tutor] the sense of brackets

Kent Johnson kent37 at tds.net
Sat Nov 22 23:04:16 CET 2008


On Sat, Nov 22, 2008 at 10:42 AM, spir <denis.spir at free.fr> wrote:
> I have long thought "[]" /simply/ is a list constructor syntax.

list(x) and [x] are not equivalent, as you have discovered. list(x)
requires that x is a sequence - something that can be iterated - and
it makes a new list  out of the elements of the sequence. If x is not
iterable then list(x) is an error; for example
In [10]: list(1)
TypeError                                 Traceback (most recent call last)
/Users/kent/<ipython console> in <module>()
TypeError: 'int' object is not iterable

OTOH [x] just takes whatever x is and makes it the single element of a list.

Kent


More information about the Tutor mailing list