Python Lists
Lists are used to store zero or more things in a variable.
One can think of the variable as, say, a grocery bag which holds "things", which
for a grocery bag would be groceries. The examples below will use this
idea of a groceries. A list is a comma delimited collection of items
placed inside brackets []
. A list can be created with items already in it:
Python
Copy Code
groceries = ['apples', 'oranges', 'paper towels']
Sometimes you don't know right away what to put in the list, so the variable can be initialized with an empty list:
Python
Copy Code
groceries = []
Lists can have more than one of the same item:
Python
Copy Code
groceries = ['apples', 'oranges', 'apples']
This is different from dictionaries, where the dictionary key is always unique.
The items in a list do not all have to be of the same type. For example, this is a perfectly valid list:
Python
Copy Code
listOfDifferentTypes = ['One', 1, True]