Lists

The first of the containers we will study

Containers hold stuff

Lists are a specific kind of container

  • Ordered
  • Heterogeneous
  • Grow to what you need

Order

  • Index positions give elements addresses
  • They start at zero.
    • It kind of makes sense. ¬†Eventually.
  • You can ask for elements by their address

Heterogeneous

  • You can put whatever you want in
  • No, seriously.
  • Anything.
  • Even more lists.

Size

  • Lists will grow as you add things to them
  • They can also be empty

Python is very different

Most languages are very strict about collections and arrays.  Python is incredibly lenient.

How do I list?

mylist = []

Lists are contained in []

You add things to lists via append:

mylist.append(something)

https://docs.python.org/2/tutorial/datastructures.html

Spend some time with the docs

Future you will thank you

List comprehensions

You will see them around.

You will be confused.

Eventually you will use them.

But don't worry about that until later.

>>> name = "Elizabeth"
>>> [letter.upper() for letter in name]
['E', 'L', 'I', 'Z', 'A', 'B', 'E', 'T', 'H']

This is a completely sane reaction.

Demo time!

Lists

By Elizabeth W.

Lists

  • 520
Loading comments...

More from Elizabeth W.