Arrays / lists Flashcards

1
Q

Add to end

A

list.append(value)

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

Remove from start

A

list.pop(0)

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

Remove from end

A

list.pop()

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

Remove from middle of array

A

del my_list[startIndex:endIndex]

Deletes a portion of the list in place, removing all elements from startIndex up to but not including endIndex

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

Length of list

A

len(my_list)

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

Map elements in a list, square each element

A

new_list = list(map(lambda x: x * 2, original_list))

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

Filter an array to create an array with elements over the value of 10

A

filtered_list = list(filter(lambda x: x > 10, original list))

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

Create an array of squares from an existing list with list comprehension

A

squares = [x**2 for x in list_variable]

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

Extend list with other list

A

list_variable.extend(another_list)

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

Count the number of times an element is in a list

A

list_variable.count(element)

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

Return the index of the first occurrence of an element, if not element not found value error

A

variable_list.index(element)

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

Reverse list in place

A

list_variable.reverse()

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

Check if element in list

A

if element in my_list:

if element not in my_list:

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

Slice and assign values in list

A

variable_list[1:3] = [7, 8]

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

Negative indexing, get last element

A

variable_list[-1]

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

Join array to make string

A

‘-‘.join(arr)

17
Q

Sort list in place

A

my_list.sort()
my_list.sort(reverse=True)
my_list.sort(key=len)

18
Q

Sort and return new list

A

sorted_list = sorted(my_list)
sorted(my_list, reverse=True)

19
Q

Calculate sum of list

A

total_sum = sum(arr)

20
Q

Remove element from array

A

arr.remove(element)