Ch. 3 Flashcards

1
Q

What is a list?

A

A collection of items/elements

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

What is an index?

A

The position that an element is in within your list, goes from 0 to n-1, where 0 is the first element. To chose the last element or to go from left to right start from -1

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

How do you change an element in a list?

A

list_name[index] = “new element” (this will replace the old element with the new one)

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

What is the simplest way to add a new element to a list?

A

list_name.append(“element you are adding”) (this adds a new element to the end of the list)

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

How do you add a new element in a specific position within a list?

A

list_name.insert(index, “new element”)

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

How do you remove an element/item from a list using it’s index?

A

del list_name[index]

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

What is the pop() Method?

A

popped_item = list_name.pop(index)
(The pop() Method removes an item in a list, but let’s you work with that item after removing it.)
where, popped_item is the new assignment
NOTE: is you leave the parenthesis blank, it will remove the last item

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

Why do we use the pop Method?

A

Sometimes you will want the value of an item after you remove it from a list.

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

How do you remove an element/item from a list without knowing it’s index and only knowing its value?

A

list_name.remove(element)

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

How do you permanently re-sort a list in alphabetical order? In reverse alphabetical order?

A

list_name.sort()
reverse: list_name.sort(reverse=True)
Remember: everything in lowercase

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

How do you temporarily re-sort a list in alphabetical order? In reverse alphabetical order?

A

sorted(list_name)

reverse: sorted(list_name, reverse=True)

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

How do you reverse a list (permenantly)?

A

list_name.reverse()

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

How do you find the length of a list?

A

len(list_name)

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

What is an index error?

A

You indexed past the length of the list

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