2.0 - Table of Contents

2.1 - What Is a List

  • A series of elements arranged in a certain order
  • can store (basically) an infinite number of data
  • data stored inside can be of different data types
  • represented with [] and the elements are separated with ,

Example: seasons.py

img

2.1.1 - Access Elements in List

  • The list is stored in a certain order
  • to access an element you need a location, or index
  • insert the index of the element in the [] after the list variable to get that element
    • list[index]
  • index starts at 0
    • e.g. the index of the third element of the list is 2
  • each element should be treated like an individual variable
    • actions of a variable can also be performed on a list Example: seasons_index.py
  • negative index will count elements backwards Example: seasons_reverse_index.py

2.2 Add, Edit, Remove Elements

  • list is dynamic
    • can be edited as code runs
    • use cases
      • inventory of character
      • users of a website

2.2.1 Edit an Element

  • similar to variables and accessing element

2.2.2 Add an Element

  • There are two major ways to add values to a list
  1. Add to the end
    • list.append(value)
  2. Add to the middle
    • list.insert(index, value)
    • all values after will increase index by 1 Example: seasons_add.py

2.2.3 Remove an Element

  • There are three ways to delete a value, each with their use cases
  1. del
  2. pop()
    • list.pop() or list.pop(index)
    • used when the value is needed for operations Example: seasons_pop.py
  3. remove()
    • lis.remove(element)
    • used when knowing the value of the element Example: seasons_remove.py

2.3 Order the Elements

  • order of elements sometimes need to be changed
  • there are two major ways to sort a list
  1. Permanent Sorting

    • list.sort()
    • permanently change the order
    • irreversible
    • list.sort(reverse=True) to reverse the order Example: seasons_sort.py
  2. Temporary Sorting

    • sorted(list)
    • temporarily reorder the list
    • can be reversed with reverse=True as well Example: seasons_sorted.py
  • There are also two very useful tools for ordering lists
  1. Reverse the List
    • list.reverse()
    • permanently reverse the order of the list
    • to restore just reverse again Example: seasons_reverse.py
  2. Check the Length
    • len(list)
    • produces the length of the list provided Example: seasons_len.py

2.4 Index Error

  • a common error when manipulating lists is IndexError: List index out of range
    • accessing index that does not exist
    • only index from -1 * len(list) to len(list) - 1 exist Example: seasons_error.py

2.5 Sample Code

seasons.py

seasons = ["spring", "summer","autumn","winter"]
print(seasons)

Output:

['spring', 'summer', 'autumn', 'winter']

seasons_index.py

seasons = ["spring", "summer","autumn","winter"]
print(seasons[0])
print("It is now " + seasons[1].title() = ".")

Output:

spring
It is now summer.

seasons_reverse_index.py

seasons = ["spring", "summer","autumn","winter"]
print(seasons[-1])
print(seasons[-2])

Output:

winter
autumn

seasons_edit.py

seasons = ["spring", "summer","autumn","winter"]
seasons[2] = "fall"
print(seasons)

Output:

['spring', 'summer', 'autumn', 'winter']

seasons_add.py

seasons = ["spring", "autumn"]
seasons.append("winter")
print(seasons)
season.insert(1, "summer")
print(seasons)

Output:

['spring', 'fall', 'winter']
['spring', 'summer', 'fall', 'winter']

seasons_del.py

seasons = ["spring", "summer","autumn","winter"]
del seasons[2]
print(seasons)

Output:

['spring', 'summer', 'winter']

seasons_pop.py

seasons = ["spring", "summer","autumn","winter"]
seasons.pop(1)
print(seasons)
seasons.pop()
print(seasons)

Output:

['spring', 'autumn', 'winter']
['spring', 'autumn']

seasons_remove.py

seasons = ["spring", "summer","autumn","winter"]
szn = "summer"
seasons.remove(szn)
print(seasons)

Output:

['spring', 'autumn', 'winter']

seasons_sort.py

seasons = ["spring", "summer","autumn","winter"]
seasons.sort()
print(seasons)
seasons.sort(reverse=True)
print(seasons)

Output:

['autumn', 'spring', 'summer', 'winter']
['winter', 'summer', 'spring', 'autumn']

seasons_sorted.py

seasons = ["spring", "summer","autumn","winter"]
print(sorted(seasons))
print(sorted(seasons, reverse=True))
print(seasons)

Output:

['autumn', 'spring', 'summer', 'winter']
['winter', 'summer', 'spring', 'autumn']
['spring', 'summer', 'autumn', 'winter']

seasons_reverse.py

seasons = ["spring", "summer","autumn","winter"]
seasons.reverse()
print(seasons)

Output:

['winter', 'autumn', 'summer', 'spring']

seasons_len.py

seasons = ["spring", "summer","autumn","winter"]
print(len(seasons))

Output:

4

seasons_error.py

seasons = ["spring", "summer","autumn","winter"]
print(seasons[4])
print(seasons[-5])

Output:

File "<seasons_error>", line 2, in <module>
IndexError: list index out of range

Homework

TBD