CSC 203 chapter 6: Dictionaries 
and Sets

0.0(0)
studied byStudied by 0 people
0.0(0)
full-widthCall Kai
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
GameKnowt Play
Card Sorting

1/41

encourage image

There's no tags or description

Looks like no tags are added yet.

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced

No study sessions yet.

42 Terms

1
New cards

dictionary

{key:value}. an unordered collection which stores key-value pairs that map immutable keys to values.

2
New cards

set

an unordered collection of unique immutable elements

3
New cards

dictionary's keys

  • Keys must be immutable and unique.

  • Multiple keys can have the same value

4
New cards

An empty dictionary can be created with

{}

5
New cards

Check if dictionary is empty

Can use a dictionary as a condition to determine if it’s empty—non-empty is True and empty is False

if dictionary:

print('dictionary is not empty')

else:

print('dictionary is empty’)

6
New cards

dictionary.items()

returns each key-value pair as a tuple

7
New cards

Adding a New Key-Value Pair

Assigning to a nonexistent key inserts a new key-value pair.

8
New cards

Remove a key-value pair with

del or pop

9
New cards

dictionary.get(key,2ndargument)

  • returns its argument’s corresponding value or None if the key is not found

  • IPython does not display anything for None.

  • get with a second argument returns the second argument if the key is not found.

10
New cards

dictionary.keys()

Returns keys

11
New cards

dictionary.values()

Returns values

12
New cards

list(dictionary.keys()) or list(dictionary.values()) or list(dictionary.items())

Converting Dictionary Keys, Values and Key-Value Pairs to Lists

13
New cards

Processing Keys in Sorted Order

for a in sorted(dictionaryname.keys()):

14
New cards

== is True if both dictionaries have

the same key-value pairs, regardless of the order in which those key-value pairs were added to each dictionary.

15
New cards

from collections import Counter

Counter()

A counter is a customized dictionary that receives an iterable and summarizes its elements

16
New cards

dictionary.update()

  • Can insert and update key-value pairs.

  • Method update also can receive an iterable object containing key-value pairs, such as a list of two-element tuples.

17
New cards

set

{ , } an unordered collection of unique values. do not support indexing and slicing. great for duplicate elimination

18
New cards

Is there a significance to iterating through a set?

No

19
New cards

To create an empty set, must use the set(), because

{} represents an empty dictionary.

  • Python displays an empty set as set() to avoid confusion with an empty dictionary {}

20
New cards

Proper subset operand?

<

{set1} < {set2}

21
New cards

What does proper subset mean?

all the elements in the left operand are in the right operand, and the sets are not equal

22
New cards

Improper subset operand?

<=

{set1} <= {set2}

23
New cards

Another improper subset method?

{set1}.issubset({set2})

24
New cards

What does improper subset mean?

all the elements in the left operand are in the right operand, and the sets might be equal

25
New cards

Proper superset operand?

>

{set1} > {set2}

26
New cards

What does proper superset mean?

all the elements in the right operand are in the left operand, and the left operand has more elements

27
New cards

Improper superset operand?

>=

{set1} >= {set2}

28
New cards

Another improper superset method?

{set1}.issuperset({set2})

29
New cards

What does improper superset mean?

all the elements in the right operand are in the left operand, and the sets might be equal

30
New cards

The argument to issubset or issuperset

can be any iterable. When either of these methods receives a non-set iterable argument, it first converts the iterable to a set, then performs the operation.

31
New cards

Union

a set consisting of all the unique elements from both sets.

The ___ operator requires two sets, but method ___ may receive any iterable as its argument (this is true for subsequent methods in this section as well).

32
New cards

Ways to find union

  • {set1} | {set2}

  • {set1}.union([list])

33
New cards

Intersection

a set consisting of all the unique elements that the two sets have in common

34
New cards

Ways to find intersection

  • {set1} & {set2}

  • {set1}.intersection([list])

35
New cards

Difference

a set consisting of the elements in the left operand that are not in the right operand

36
New cards

Ways to find difference

  • {set1} - {set2}

  • {set1}.difference([list])

37
New cards

set.add()

inserts its argument if the argument is not already in the set; otherwise, the set remains unchanged

38
New cards

set.remove()

removes its argument from the set—raises a KeyError if the value is not in the set

39
New cards

set.discard()

removes its argument from the set but does not cause an exception if the value is not in the set

40
New cards

set.pop()

Can remove an arbitrary set element and return it with pop

41
New cards

set.clear()

Empties the set

42
New cards

Set comprehensions are defined in

{}