Lists - Hsanokklis/2023-2024-Tech-journal GitHub Wiki

The List Data Type

A list is a value that contain multiple values in an ordered sequence. The term list value refers to the list itself not just the values inside of the list value.

This is what a list value looks like:

['cat', 'bat', 'rat', 'elephant']

  • A list can be distinguished by []
  • Values inside a list are called items
    • items are separated with commas

Example

Say you have the list ['cat','bat','rat','elephant'] stored in the variable spam

  • The code spam[0] would evaluate to 'cat' and spam[1] would evaluate to 'bat'

The integer inside the brackets that follows the list is called an index. The first value in the list is at index 0, the scond value is at index 1, the third value is at index 2, and so on.

image

image

The expression 'Hello' + spam[0] evaluates to 'Hello' + 'cat'

  • Python will give you an IndexError error message if you use an index that exceeds the number of values in your list value ie spam[100000]

  • Indexes can only be integer values and not floats

    • spam[1.0] will not work
    • spam[1] will work
  • lists can contain other values and the values of the lists can be accessed using multiple indexes

image

In the example above we use spam[0][1] to access both of the lists values

  • the first index indicates which list to use
  • the second index indicates the value within the list value

spam[0][1] will print bat

  • this means its choosing from the first list and then choosing the first index which is bat

Negative Indexes

Indexes start at 0, but you can still use negative numbers for the index.

  • The index -1 refers to the last index in a list
  • The index -2 to the second-to-last index in the list and so on

image

if you return spam[-1] it will print elephant

Getting Sublists with Slices

A slice can get several values from a list, in the form of a new list.

  • spam[2] this is an index (one integer)
  • spam[1:4] this is a slice (2 integers)

In a slice

  • the first integer is the index where the slice starts.
  • The second integer is the index where the slice ends.

A slice goes up to, but will not include the value at the second index. A slice evaluates to a new list values.

image

as a shortcut you can just leave out one or both of the indexes on either side of the colon in the slice.

  • Leaving out the first index is the same as using 0
  • Leaving out the second index is the same as using the length of the list (or slicing to the end of the list)

image

Getting a List's Length with len()

The len() function will return the number of values that in a list value passed to it, just like counting the number of characters in a string value.

image

Changing Values in List with Indexes

Normally a variable name goes on the left side of an assignment statement like spam = 42, however you can use an index of a list to change the value at that index.

Example

spam[1] = 'aardvark'

  • means assigns the value at index 1 in the the list spam to the string aardvark.

image

List Concatenation and List Replication

The + operator can combine two lists to create new list values in the same way it combines two strings into a new string value. The * operator can also be used with a list and an integer value to replicate the list.

image

Removing Values from Lists with del Statements

The del statement will delete values at an index in a list. All of the values in the list after the deleted value will be moved up one index.

image