Lists Flashcards

1
Q

Lists are a fundamental data structure in Python that allow you to “_____” & “________” collections of items

A

Store & manipulate

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

They are “_______” and “____” hold items of different data types, such as numbers, strings, or even other lists

A

Versatile; and

COMPARED to strings = which are limited

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

Lists are “_______”, meaning you can modify their elements by adding, removing, or changing values

A

Mutable

COMPARED to strings which are not mutable

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

How do you create a list?

A

Are created by enclosing items in square brackets [ ] and separating them with commas

Can make “mixed” lists

Ex.
List of 5 integers:
numbers = [10, 20, 30, 40, 50]

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

How can list elements be accessed?

Indexing starts at “__”

A

Can be accessed using index values

Similar to characters in a string

Indexing starts at 0

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

How do you access multiple elements in a list?

Give an example

A

You can use list slicing which allows you to extract a portion of a list

Syntax: my_list[start:end:step]
- start is the starting index
- end is the ending index (exclusive)
- step is the step size

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

How do you modify list elements after they were initialized?

Give an example

A

Elements can be modified by assigning a new value to a specific index

Ex.
my_list = [10, 20, 30, 40, 50]
Modifying the second element: my_list[1] = 25
Resulting list: [10, 25, 30, 40, 50]

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

True or false. Lists support various operations, such as concatenation, repetition, and membership testing (similar to string operations)

A

True

Concatenation: Combining two or more lists using the + operator.

Repetition: Repeating a list a certain number of times using the * operator.

Membership testing: Checking if an item is present in a list using the in and not in operators.

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

What are 3 different ways to traverse a list with loops?

A
  1. With for loop iterating over list items:
    for i in list:
    print(i)
  2. With for loop using range and indexing:
    for i in range(len(list)):
    print( list[i] )
  3. With while loop using indexing:
    length = len(list)
    i = 0
    while i < length:
    print(list[i])
    i += 1
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

How is the append() method used for lists?

Give an example

A

Used to add an element to the end of a list

This method modifies the list in-place, meaning it alters the original list rather than creating a new one

list_name.append(element)

SIMILAR to the use of the += operator

Ex.
numbers = [1, 2, 3, 4]
numbers.append(5)

Result [1, 2, 3, 4, 5].

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

If you want to combine MULTIPLE lists the “____” operator is more efficient

A

+=

list = [1,2,3,4]
toadd = [5,6]
list += toadd

[1, 2, 3, 4, 5, 6]

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

If you want to combine a SINGLE element to a list the “______” method is a better choice

A

.append()

list = [1,2,3,4]
toadd = [5,6]
list.append(toadd)

[1, 2, 3, 4, [5, 6]]

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