A list is used to store values of any type.  Here are some examples of lists and the ways you can manipulate them:

 

 

 

 

Looping through items in a list

If you want the items to print next to each other use a comma after the print statement

Some Things You Can Do With Lists

Find its length:

Find out if something is in a list:

Slice it:

Concatenate lists:

 

 Lists are Mutable

Do you remember earlier we said that strings were not mutable ie they could only be replaced, not changed?

One of the big differences with lists is that they are mutable.  You can change individual items in a list.  In our list of colours, I have changed green to purple.

Deleting Items

Because lists are mutable, you can delete items using del

You can also delete a list slice  eg del colours[1:3]

 

More Things You Can Do

Reverse a list:

Sort a list:

Count items:

Convert a List to a String:

Use join which joins values in a list, with a separator.  If you want no space, use quote marks with no space between as a separator ""

Convert a String to a List:

If you want each character as a separate list item, use list()

If you need a sentence split into individual words, use split()

 

How To Read in Items for a List from User Input

and here is the result:

Creates a new blank list called lines
 Reads in the first item of user input and stores it in a variable called line
The program will continue to loop while line is not equal to nothing
Add the line that the user input onto the end of the list
Get the next line of user input