Python Dictionaries

0.0(0)
studied byStudied by 0 people
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
Card Sorting

1/14

flashcard set

Earn XP

Description and Tags

Flashcards covering key concepts of Python dictionaries, including creation, manipulation, and methods.

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced

No study sessions yet.

15 Terms

1
New cards

Dictionary

An object that stores a collection of data in key-value pairs. Keys must be immutable objects.

2
New cards

Retrieving a Value

Use dictionary[key]. Raises KeyError if key is not found. Use 'in' and 'not in' to check for key existence.

3
New cards

Adding Elements

Dictionaries are mutable. Add key-value pairs using dictionary[key] = value. Overwrites if key exists.

4
New cards

Deleting Elements

Use del dictionary[key]. Raises KeyError if key is not found.

5
New cards

len() function

Used to obtain the number of elements in a dictionary.

6
New cards

Key Constraints

Keys must be immutable objects, but values can be of any type.

7
New cards

Creating Empty Dictionary

Use {} or dict(). Add elements as program executes.

8
New cards

Iterating with For Loop

Use for key in dictionary: to iterate over keys.

9
New cards

clear() method

Deletes all elements in a dictionary, making it empty.

10
New cards

get() method

Returns value associated with specified key. Returns default (or None) if key is not found, avoiding KeyError.

11
New cards

items() method

Returns all keys and their associated values as a dictionary view (sequence of tuples).

12
New cards

keys() method

Returns all the dictionary's keys as a sequence.

13
New cards

values() method

Returns all the dictionary's values as a sequence.

14
New cards

pop() method

Returns value associated with specified key and removes the key-value pair. Optional default value if key not found.

15
New cards

popitem() method

Returns and removes the last added key-value pair as a tuple.