Lists And 2D Lists Flashcards

1
Q

What is a list in Python?

A

A list is a data structure that can store multiple items in one variable.

Example: my_list = [1, 2, 3].

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

How do you access an item in a list?

A

Use the index, starting from 0.

Example: my_list[1] returns 2.

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

How do you change an item in a list?

A

Assign a new value to an index.

Example: my_list[0] = 10 changes the first item to 10.

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

How do you add an item to the end of a list?

A

Use append().

Example: my_list.append(4) adds 4 to the end.

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

How do you insert an item at a specific position in a list?

A

Use insert(index, value).

Example: my_list.insert(1, 99) adds 99 at index 1.

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

How do you remove an item from a list by value?

A

Use remove(value).

Example: my_list.remove(3) removes the first 3.

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

How do you remove an item from a list by index?

A

Use pop(index) or del my_list[index].

Example: my_list.pop(2) removes the item at index 2.

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

How do you loop through a list in Python?

A

Use a for loop like for item in my_list: to access each item one at a time.

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

What is a 2D list in Python?

A

A list of lists.

Example: matrix = [[1, 2], [3, 4], [5, 6]].

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

How do you access an item in a 2D list?

A

Use two indexes.

Example: matrix[1][0] returns 3.

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

How do you change an item in a 2D list?

A

Use two indexes to assign a value.

Example: matrix[2][1] = 9.

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

How do you loop through a 2D list in Python?

A

Use nested loops: for row in matrix: and then for item in row:.

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

What are common uses of 2D lists?

A

Representing data tables, grids, spreadsheets, or game boards like tic-tac-toe.

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

What is the difference between a list and a 2D list?

A

A list stores a single sequence of items. A 2D list stores lists within a list, forming rows and columns.

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