Lecture 3 Flashcards

dictionaries - defining, creating, accessing, methods for modification + copying of dictionaries

1
Q

define dictionary

A

A data structure that consists of key-value pairs.

We use the keys to describe our data and the values to represent the data.

Keys are usually numbers or strings; values can be any data type

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

strip method syntax+ function

A

stringname.strip(characters to remove)

removing characters from the beginning or end of a string for characters supplied as arguments

OR

if no argument passed, will remove only leading and trailing spaces in a string

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

how can you add a key-value pair to a dictionary?

A
  1. define dictionary d={}
  2. d[“key1”] = “value1”
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

how can you initialize a dictionary using typecasting?

A

d = dict()

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

how do you combine 2 lists into a dictionary using iteration and one variable?

A
  1. define both lists
  2. define range + variable
  3. key-value assignment formatting: d[l1[i]] = l2[i] NOTE brackets around key, none around value. if you add brackets around the value it will print with brackets!*
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

how do you use a range loop to print ONLY the values in a dictionary?

A

dictionaryname={}
for i in dictionaryname.values():
print(i)

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

how do you use a loop to print ONLY the keys in a dictionary?

A

dictionaryname={}
for i in dictionaryname.keys():
print(i)

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

how do you use a loop to print the KEY-VALUE PAIRS in a dictionary?

A

dictionaryname={}
for i in dictionaryname.items():
print(i)

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

how do you print a LIST of TUPLES containing the key-value pairs in a dictionary?

A

print(list(dictionaryname.items()))

NOTE: you must typecast to a list or it will auto preface with “dict_items”. will still print a list of tuples

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

mutability and ordered/unordered? [ ]

A

[] - mutable and ordered

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

mutability and ordered/unordered? { }

A

{} - unordered, mutable, key-value pairs

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

mutability and ordered/unordered? “ “

A

”” - ordered, immutable

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

mutability and ordered/unordered? ( )

A

() - ordered, immutable

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

how do you access the values in a dictionary?

A

using the corresponding keys

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

update method function and syntax

A

appends dictionary 2 to the end of dictionary 1. can also pass another iterable object (usually of tuples containing key-value pairs)

dict1.update(dict2)

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

fromkeys method function and syntax

A

fromkeys creates a dictionary from the given sequence of keys and one value argument

dict.fromkeys(key list, value)

17
Q

how do you use a range loop to create a dictionary using indexing?

A

names = [“john”, “ala”, “ilia”, “sudan”, “mercy”] #define list of keys
marks = [100, 200, 150, 80, 300] #define list of values
dictionary: d = {} #define dictionary
for i in range([# of key-value pairs-1]):
d[names[i]] = marks[i]

18
Q

how do you use a range loop to create a list of lists where the pairs are assigned in order?

A

l1=[x num of elements]
l2=[x num of elements]

l3=[]

for i in range len(l1):
l3.append(l1[i], l2[i])

19
Q

how do you use a range loop to create a dictionary using 2 defined variables in the range statement?

A

dict1={}

for (i,j) in l:
dict1[i] = j

20
Q

how do you remove a key-value pair from a dictionary?

A

dictionaryname.pop(keyname)

21
Q

how do you rename a key in a dictionary?

A

d[‘newkey’] = d.pop(“oldkey”)

22
Q

how do you use a dictionary to count the number of occurrences of an element?

A

l=”Hello world”
x=0
d=dict.fromkeys(l, x) #create a dictionary using fromkeys and initialize every occurrence to 0. no duplicate keys by default

for i in l: #loop across sequence
d[i] = d[i]+1 #add 1 to the key-value pair for every occurrence of the sequence

23
Q

define set. ordered or unordered? mutable/immutable?

A

a collection of unique data, meaning that elements within a set cannot be duplicated.

UNORDERED and MUTABLE

24
Q

add method syntax + function

A

used to add an element to a set.

setname.add(element)

25
Q

remove/discard methods syntax + function

A

remove() will raise an error if the specified item does not exist, and discard() will not.

26
Q

copy method syntax + function

A

returns a copy of the original set. As the copied list is shallow, any changes made to the new list will not be reflected in the original list.

setname.copy()

27
Q

clear method syntax + function

A

Removes all elements from the set

setname.clear()

28
Q

intersection method syntax + function

A

Finding the values present in both the sets

set1.intersection(set2)

29
Q

difference update method + function

A

Given two Python sets, update the first set with items that exist only in the first set and not in the second set.

s1.differenceupdate(s2)

you can also pass a full set through s2:

s2.difference_update({7,17})

30
Q

symmetric difference update syntax and function

A

symmetric_difference_update returns a new set containing only the elements that are NOT common to both sets. like the non-overlapping parts of a venn diagram.

A.symmetric_difference_update(B)

31
Q
A