Data Structures Flashcards

You may prefer our related Brainscape-certified flashcards:
1
Q

In a 2D array, how would you access an element within an array inside the array?

A

ex: a_list[10][0] accesses the first element of the eleventh array

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

Does the ‘in’ keyword work with nested loops?

A

No. You must check specific arrays. Ex: if a = [1, [2, 3], 4], then (2 in a) returns false.

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

How do you remove something from an array?

A

Using ‘del’ to delete a specific value, or the entire array. Ex: del a[0] deletes the first element of a. del a deletes the entirety of a.
Using ‘pop(array)’ to remove an element from an array and return it.

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

How do you sort an array? (using built in functions)

A

array.sort() sorts the array. This directly changes the array.

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

What is another term for dictionaries?

A

Associative arrays.

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

What are dictionaries used for?

A

Storing data in key:value pairs. They do not allow duplicate keys.

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

How do you change, add, or delete values from a dictionary?

A

Using ‘del’ removes a value (or the whole dictionary).
dict[‘key’] = ‘value’ will add a new value or change a preexisting one with the same key.

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

How do you access the value with a key in a dictionary?

A

Ex. bob = {‘age’ : 27, ‘job’: CEO}
bob[‘age’] accesses 27.

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

How do you determine the length of an array?

A

len(). Works with dictionaries as well.
Ex. a = [1, 2, 3], len(a) returns 3

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

How does .keys() and .values() work?

A

They return a list of the keys and values of a dictionary, respectively.

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

How does .items() work?

A

Returns the items of a dictionary as tuples. [(key,value),(key2,value2)…]

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

How does ‘in’ work with dictionaries?

A

It checks for keys.

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

How would you copy a dictionary to a new variable?

A

dict1 = dict
dict2 = dict1.copy() #dict2 is now its own thing, not a reference to dict1

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

What does popitem() do?

A

Removes the last insterted key-value pair of a dictionary

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

What does .clear() do?

A

Removes the elements from a dictionary.

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

What does .get() do?

A

The value of the key of a dictionary. ex: dict1.get(‘keyname’) returns value

17
Q

What does .setdefault() do?

A

Returns the value of the specified key. If the key does not exist, makes a new key:value.
dict1.setdefault(‘keyname’, ‘value’)

18
Q

What does .update() do?

A

Includes the key:value pairs of a dictionary in another.
ex:
dict1 = dict
dict1.update(dict2) #dict1 now contains the content of dict1 and dict2

19
Q

What does sorted(dict) do?

A

Returns a dict but sorted alphabetically.

20
Q

How do you slice a list?

A

ex:
list1 = [a, b, c, d, e, f, g, h, i]
list1[:3] #the first 3 items of list1
list1[3:] #d, and following
list1[2:5] #from 2 (inclusive) to 5 (non inclusive)

21
Q

Are strings and lists mutable?

A

Lists are mutable. Their values can be directly changed.
Strings are not. Their indexes cannot be directly changed.

22
Q

What does .append() do?

A

Adds a value to the end of a list.

23
Q

What does .pop() do?

A

Pops the last item, or item at specified index

24
Q

What does .reverse() do?

A

Reverses the list.

25
Q

What does .index() do?

A

Returns the index of the first instance of the specified item.

26
Q

What does .count() do?

A

Counts the instances of a value in a list.

27
Q

What does remove do?

A

Removes the first instance of an item.

28
Q

What does .split() do?

A

With strings, divides strings into words (whitespace by default is seen as a divider, but another string can be specified)

29
Q

What does .join() do?

A

Adds strings together. A specified string can be used to join them.

30
Q

What does sum() do?

A

Returns the sum of the items in a list.

31
Q

What does min() do?

A

Returns the minimum value in a list.

32
Q

What does max() do?

A

Returns the maximum value in a list.

33
Q

How does list comprehension work?

A

Lets loops create a list.
Ex: list1 = [i for i in range(10)] #list of numbers 0 through 9.
Additional operations can be added, such as if.
Ex: list2 = [i for i in L if i<=0] #All values of L less than or equal to 0.

34
Q

How do you make a copy of a list?

A

M is a list.
L = M #doesn’t work
N = M[:] does work.