List Operations

Append

Insert

Allows you to place a value somewhere arbitrarily within a list.

Q: How would you, for example, prepend a value to a list?

Sort

.sort is an in place operation. Meaning, it does not return a new list. The list itself is modified, leaving you with the same list object, but with sorted elements within. This is because a list is a mutable object, so why not take advantage of this property.

If you accidentially stored the return value from .sort, it would be the value None. It's definitely not what you want.

The Wrong Way

The Right Way

Reverse

.reverse is also an in place operation. Do not expect any return value form it. The same warnings given for .sort apply here.

Index

Find the index of the first matching value provided.

Count

Counts all occurrences of a value in a list.

Remove

Like .index, this will find the first occurrence of a value, and remove it from the list. The list is modified in place.

Pop

A kind of deletion, except the value being removed is returned to you.

Extend

The adds one list to another. Think 'concatenation' or a better 'bulk append', except a new list is not returned.

The list passed into .extend is not modified after it is used to extend the other list.

Deleting values from a list

Introducing the del keyword

Aliasing and Deleting (aka reference counting)

A different order