Lists, Tuples, Dictionaries, and Sets Flashcards

1
Q

what command do we use to add multiple new elements to a list?

A

listname.extend([item 1, item 2])

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

what command do we use when we want to add one new nested element to a list?

A

listname.append([item 1, item 2])

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

what is the major difference between lists and tuples?

A

list items can be individually changed by using listname[item number] = new value

tuples cannot be changed, but instead a new variable must be made such as sortedtuple

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

what command removes a single item from a list

A

del listname[0]

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

true or false: if you have aliased lists and you change an item on list A, it will change on list B

A

true - they are both pulling from the same object

clone the list to fix the problem

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

what is the command to clone a list

A

B = A[:]

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

how do you sort the values in a tuple

A

newtuple = sorted(tuple)

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

how do you access the nested tuple within a tuple

A

tuple[1][2]

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

how do you locate the index of a particular string or item?

A

tuplename.index(“item name”)

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

how do you create a dictionary?

A

Dict = {key1:value 1, key 2:value 2}

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

how do you recall a value from the dictionary using its key

A

Dict[“key1”]

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

what is the command to get all the keys in a dictionary?

A

dictionaryname.keys()

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

what is the command to get all the values in a dictionary?

A

dictionaryname.values()

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

how do you add a key value pair into a dictionary?

A

dictionaryname[“new key”] = “new value”

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

how do we delete an entry in a dictionary with its key

A

del(dictionaryname[“key”])

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

how do we check if a key is in a dictionary?

A

“key name” in dictionaryname

returns True or False

17
Q

how do you convert a list to a set?

A

newset = set(old_list)

18
Q

how do we add something to a set?

A

setname.add(“new item”)

19
Q

how do we get rid of something in a set?

A

setname.remove(“item to be removed”)

20
Q

how do we isolate common elements between sets

A

set1 & set 2

21
Q

how do we find items only contained in set 1 but not set 2?

A

set1.difference(set2)

22
Q

what is the command to find the crossover between two sets without combining them?

A

set1.intersection(set2)

23
Q

how do you combine every element of two sets into one big set

A

set1.union(set2)

24
Q

how do we check if a set is fully contained within a different set?

A

smallset.issubset(largeset)

or

set({“item 1”, “item 2”}).issubset(largeset)

25
Q

how do we check whether a set in contained in a superset

A

largeset.issuperset(smallset)