List Basics

Lesson on Lists from MIT

Lists are a collection data item values.

  • List is a built-in data type in Python 3

  • Each item can be same or different data type (Int, Float, String, Boolean, List, and etc)

  • Each items are separated by a comma except the last

  • A list is denoted by square brackets: [ ]

  • A list is iterable; therefore, we can traverse through it with a for loop

  • Lists are compatible with the following built-in functions:

    • str() and tuple() :: can be converted to these data-types easily

    • len() :: returns the size of your list

    • enumerate() :: to help you pair index and items

    • reversed() :: will create a flipped iterator object

    • sorted() :: will help you return a sorted version of the list

    • min() and max() :: will help you determine the least and greatest value within a list, and compare lists

    • sum() :: in a list composed of numeric values, sum() will add up all the values

    • many more, but they are deemed advanced and requires their own lessons

Examples:

Generating Lists from Sequences

list() function:

  • Converts the argument into a list

  • The argument should be either a sequence-like data (example: strings)

Traversal & Accessing a List

To Traverse: to travel across; helps us get through our data.

Lists are indexable

Similar to strings, we can look at an individual value at an index location; returns a value.

We can also traverse by index:

Lists are Sliceable

We can look at portions of a list by slicing; slicing returns a sample of the list back.

Slicing = [start:end:step] step is 1 if not defined

Last updated