M03 - Lists Flashcards

1
Q

List definition and what can be in a list

A
  • Array that contains multiple data items
  • Can be data types such as integers, floating-point decimals, strings, and Boolean values as well as lists, tuples, and dictionaries
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

3 Properties of List

A
  1. We can use indexing and slicing to retrieve specific items from the list
  2. We can add or remove items from a list, which makes lists dynamic data structure
  3. We can change the contents in a list. This means lists are mutable: we can change one or more items in a list to something else.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

List Syntax

A

list_variable = [‘Item1’ , ‘Item2’ , ‘Item3’ , ‘Etc….’]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Empty List Syntax

A

empty_list = [ ]

empty_list = list( )

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

3 general rules for indexing

A
  1. Each item in a list has an index that specifies its position in the list
  2. Indexing starts at 0. Therefore, the index of the first item is 0, second is 1, third is 2, etc.
  3. Because indexing begins at 0, the index of the last item in a list is 1 less than the total number of items in the list
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Get FIRST indexed item from a list syntax

A

list_variable[0]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Negative Index def + syntax to get LAST item

A

-Used to identify list item’s position relative to the end of a list

list_variable[-1]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Length of a list syntax

A

len(list_variable)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Slicing Format and explanation

A

list_variable[start : end]

  • start is the first index item in slice
  • end is index marking end of slice
  • expression returns list containing from start UP TO BUT NOT INCLUDING end index value
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Slice to get index item 3 until the end

A

list_variable[2:]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Slice to get item 5 until the front

A

list_variable[: 5]

^have to start 1 AFTER whatever you want

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Add items to a list options

A

append( )

insert( )

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

append( ) Syntax + where item goes

A

list_variable.append(‘New Item’)

Goes to end (highest index) of the list

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

insert( ) Syntax + where item goes

A

list_variable.insert(index , “New Item”)

Goes to designated position in index

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Discard items from a list options

A

remove( )

pop( )

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

remove( ) Syntax + how it works

A

list_variable.remove(“Item”)

Removes 1st (lowest index value) instance of this item, if there are multiples whichever has lowest index is removed

17
Q

pop( ) Syntax + how it works

A

pop(index)

Removes the indexed value from list and prints it

18
Q

Change an element in a list syntax + what it does

A

list_variable[index#] = “New Value”

Replaces existing index value with whatever is on the right hand side of the index