Functions that work with or create lists

The list() function creates a copy of a list. It also turns any other sequence object or iterable into a list. (An iterable is an object whose elements can be retrieved one at a time.) This example turns a tuple into a list:

>>> mytuple = ('apple', 'orange', 'pear') >>> mylist = list(mytuple) >>> mylist

If you feed a string to the list() function, it makes a separate list item out of each character, as in this example.

>>> mystring = "Boo!" >>> list(mystring)

To find out the number of elements in a list, use the len() function (it also works on other data types).

Was this article helpful?

0 0

Post a comment