This Lesson is for Members

Subscribe today and get access to all lessons! Plus direct HD download for offline use, enhances transcripts, member comment forums, and iTunes "podcast" RSS feed. Level up your skills now!

Unlock This Lesson

Already subscribed? Sign In


    Use Lists in Python

    Will ButtonWill Button

    You may already be familiar with arrays. In Python, they are known as lists. Learn how to create, append, remove, sort, and reverse lists in this lesson.



    Become a Member to view code

    You must be a Member to view code

    Access all courses and lessons, track your progress, gain confidence and expertise.

    Become a Member
    and unlock code for this lesson


    If you've used other programing languages, you may know these as arrays, but in Python, they are called lists. It's just a sequence of objects. It doesn't have to be the same data type, though. We can have a list that has an integer, a string, another integer. It can even contain a dictionary.

    Individual elements can be accessed by their index numbers. To grab the first item from the list b, we can use square brackets and index number. To grab the second number, we would use the index number one.

    Let's create an empty list called pets. We'll do that by specifying empty braces. Then we can add to the pets list with the append method. We can add a cat to it, we'll add a dog, we can add a bear to it, and we can add a shark. When we display the list, all of our items are added to it.

    If we want to get rid of the dog, we can use the remove method, and specify the item that we want to remove. We can use the pop method to remove the last item from the list. If we want to specify which item we'd like to get rid of, we can call the pop method, and pass it an index number.

    We add our pets back in. We can sort the list, which is going to sort the items alphabetically. You can reverse the list as well.

    To find out how many items are in the list, you can use the LEN method, which is short for length. You can get the number of occurrences of a specific item in the list using the count method, passing in the list item that you would like to count, and it returns the number of instances found.