List Flashcards

1
Q

returns the length of a list

A

len(my_collection)

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

Add multiple items to a list

A

my_collection.extend([ More”, “Items”])

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

Add a single item to a list

A

my_collection.append(“Single”)

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

1.Delete the object of a list at a specified index
2. Delete item by value don’t return
3.Delete and returns an item at a given index

A
  1. del my_list[2]
  2. my_list.remove(“apple”)
  3. my_list.pop(“apple”)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Clone a list 3 ways

A

clone = my_collection[:]
clone = list(original_list)
clone = original_list.copy()

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

Concatenate two lists

A

my_collection_2 = [“a”, “b”, “c”]

my_collection_3 = my_collection + my_collection_2

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

Calculate the sum of a list of ints or floats

A

number_collection = [1,2,3,4.5]

sum(number_collection)

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

Check if an item is in a list, returns Boolean

Check if an item is not in a list, returns Boolean

A

item in my_collection

item not in my_collection

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

Removes all the elements from the list

A

list.clear()

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

Returns the index of the first element with the specified value

A

my_list = [3,5,6,5,7]
my_list.index(5) returns 1, the index of the first occurrence of 5.

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

Returns the number of elements with the specified value.

A

my_list = [3,5,6,5,7,5]
my_list.count(5) returns 3

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

Sorts the list and reverse it

A

list.sort().reverse()

new_list = sorted(example_list) returns a new sorted list

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

Min Max and Sum

A

e_list = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5]

min(e_list) returns 1
max(e_list) returns 9
sum(e_list) returns 44, the sum of the items in the list.

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

Returns True if all elements of the list are true (or if the list is empty)

Returns True if any element of the list is true. If the list is empty, returns False.

A

all([True, True, False]) returns False, as not all elements are True.

any([True, False, False]) returns True, as at least one element is True.

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

Converts an iterable (tuple, string, set, dictionary) to a list.

A

tuple_k = (4,5,6)
my_list = list(tuple_k)

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

Slices a part of the list.

A

e_list = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5]

new_list = e_list[2:5]
slices the part [4, 1, 5] from the list.