u03-slides-data-structures-flashcards
What are the main types of data structures in Python?
Sequence types (list, tuple, string, range), unordered collections (set), and mapping types (dictionary)
How is a list created in Python?
Using square brackets containing comma-separated values (e.g.
What is special about Python lists?
They are mutable, can contain mixed data types, can be nested, and preserve order
At what index do Python sequences start?
0
What is the difference between lists and tuples?
Tuples are immutable (cannot be changed after creation), while lists are mutable
How are tuples created in Python?
Using comma-separated values, optionally enclosed in parentheses (e.g., my_tuple = 42, “text”, 3.14)
What is a set in Python?
An unordered collection of unique elements, created using set() or curly braces
Set:
A collection of unordered, mutable, and unique elements.
Defined using curly braces {} or the set() constructor.
Example:
my_set = {1, 2, 3}
my_set = set([1, 2, 3]) # Alternative
What are common set operations in Python?
Union (|), intersection (&), difference (-)
What is a dictionary in Python?
A mutable and ordered collection of key-value pairs
How are dictionaries created in Python?
Output: {‘key1’: ‘value1’, ‘key2’: ‘value2’, ‘key3’: ‘value3’}
1. Using Curly Braces
my_dict = { "key1": "value1", "key2": "value2", "key3": "value3" } print(my_dict)
2. Using the dict() Constructor
my_dict = dict(key1="value1", key2="value2", key3="value3") print(my_dict) # Output: {'key1': 'value1', 'key2': 'value2', 'key3': 'value3'}
3. From a List of Tuples
- Convert a list of key-value tuples into a dictionary:
my_dict = dict([("key1", "value1"), ("key2", "value2"), ("key3", "value3")]) print(my_dict) # Output: {'key1': 'value1', 'key2': 'value2', 'key3': 'value3'}
4. Using Dictionary Comprehension
- Create a dictionary dynamically:
my_dict = {x: x**2 for x in range(1, 4)} print(my_dict) # Output: {1: 1, 2: 4, 3: 9}
5. From Two Separate Iterables
- Use the zip() function to pair keys and values:
keys = ["key1", "key2", "key3"] values = ["value1", "value2", "value3"] my_dict = dict(zip(keys, values)) print(my_dict) # Output: {'key1': 'value1', 'key2': 'value2', 'key3': 'value3'}
6. Empty Dictionary
- Create an empty dictionary:
my_dict = {} print(my_dict) # Output: {}
What can be used as dictionary keys?
Any hashable object
What can be used as dictionary values?
Any object
What is a list comprehension?
Explain the key concepts!
Output: [0, 1, 4, 9, 16]
A compact way to create a list by looping over an iterable, with optional filtering and transformations
Key Concepts:
- Basic Syntax:
[expression for item in iterable]
- With Filter:
[expression for item in iterable if condition]
- Transform and Filter Together:
[transform for item in iterable if condition]
Create a list of squares using list comprehension (base values zero to five)
Simple List Comprehension
A list comprehension creates a new list by iterating over an existing sequence.
Example 1: Create a list of squares
squares = [x**2 for x in range(5)] print(squares)
Filter even numbers using list comprehension for the integer numbers from zero to ten
Output: [0, 2, 4, 6, 8]
List Comprehension with Filters
You can add a condition to include only certain elements in the resulting list.
Example 2: Filter even numbers
even_numbers = [x for x in range(10) if x % 2 == 0] print(even_numbers)