Lists

Python lists are mutable sequences. They are very similar to tuples, but they don't have the restrictions of immutability. Lists are commonly used to storing collections of homogeneous objects, but there is nothing preventing you from store heterogeneous collections as well. Lists can be created in many different ways. Let's see an example:

>>> []  # empty list[]>>> list()  # same as [][]>>> [1, 2, 3]  # as with tuples, items are comma separated[1, 2, 3]>>> [x + 5 for x in [2, 3, 4]]  # Python is magic[7, 8, 9]>>> list((1, 3, 5, 7, 9))  # list from a tuple[1, 3, 5, 7, 9]>>> list('hello')  # list from a string['h', 'e', 'l', 'l', 'o']

In the previous example, I showed you how to create a list using different techniques. I would like you ...

Get Getting Started with Python now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.