Exploring Lists in Python 3

Lists are containers that can store anything

Creating lists

Examples:

Empty = []

Nums = [10, 20, 30, 40.5]

Words = ['hello', 'world', 'chicken']

Mixed_type = ['hello', 22, 'world', 22.323]

Each entry is assigned an index starting at 0, for example given the mixed_type list above:

Mixed_type[0] = 'hello'

Mixed_type[1] = 22

Mixed_type[2] = 'world'

Mixed_type[3] = 22.323

Slicing a list

We can also extract a slice from the list (a range) using a colon:

Mixed_type[1:3] = [22, 'world']

Appending a list

Lets start with an empty list:

New_list = []

To add items to the list, we use the append function:

New_list.append('hello')

The list then becomes:

New_list = ['hello']

Removing items from a list

Given the following example:

newList = ['hello', 22, 'world', 45.5]

To remove a known item from the list by name or exact value:

newList.remove('hello')

To delete using the index:

del newList[0]

You can also delete by slice:

// Delete the first 2 items

del newList(:2)