Arrays (lists) Flashcards

1
Q

What are arrays called in Python?

A

Lists

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

If I tried to append and pop from a Python list, would this throw an error?

A

No, Python lists are dynamic and can be used like stacks

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

How would you insert a value into a Python list?

A

arr.insert(1, 7)

This will insert the value 7 into the index 1

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

What is the time complexity of arr.insert()?

A

O(n) because if we insert a value into the first index of an array, we have to shift every other value which is the length of the list in the worst-case, thus O(n)

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

True or False: Python lists are considered arrays as well as stacks

A

True

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

What is the better alternative to using arr.insert()?

A

arr[0] = 0

In Python we can reassign elements in a list in constant time

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

If we wanted to initialize n to be equal to [1, 1, 1, 1, 1] - how would we do this in Python?

A

n = [1] * 5

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

How can we return the last element of a list in Python?

A

arr[-1]

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

How can we create a sublist from the following list?

arr = [1, 2, 3, 4, 5]

A

arr[1:3]

Result will be [2, 3]

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

How can we unpack a list in Python (assign variables to elements in the list)

A

a, b, c = [1 ,2 ,3]

Result will be a = 1, b = 2, c = 3

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

How would we loop through the elements in a Python list and get their values?

A

for i in range(len(nums)):
print(nums[i])

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

How would we loop through values only in a Python list?

A

for num in nums:
print(num)

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

How can we loop through a list and get both the index and the value in Python?

A

for i, num in enumerate(nums):
print(i, num)

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

How would we loop through multiple arrays simultaneously and unpack them?

A

Can use zip:

for n1, n2 in zip(nums1, nums2):
print(n1, n2)

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

How do we reverse the elements in a list without creating a new list?

A

nums.reverse()

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