Sets & Dictionaries
Sets
1. Describe sets.
A set is an unordered collection of unique elements.
2. How to create a set? Provide an example.
You can create a set by enclosing a comma-separated sequence of values within a curly brackets{}.
Example: set = {1,2,3,4}
3. Can a set store elements of any datatype?
No, sets can only store data types that are immutable, such as numbers, strings and tuples.
4. Explain the use-cases and the key difference between st.remove(x) and st.discard(x).
Both methods are used to discard the specified value in the set. The key difference is that if the value does not exist,
remove() will return an error whereas discard() will return none.
5. Explain union, intersection, differences, symmetric differences and provide the syntax to perform the operations.
- Union: Returns a set that combines the items from two sets. Keeping only a single instance of any repeating elements.
- Syntax:
S1 | S2
- Syntax:
- Intersection: Returns a set that only contains the elements common to both sets.
- Syntax:
S1 & S2
- Syntax:
- Difference: Returns a set that contains the elements of one set without the elements of the other set.
- Syntax:
S1 - S2
- Syntax:
- Symmetric Difference: Returns a set that represents the union of both sets without the intersection of both sets.
- Syntax:
S1 ^ S2
- Syntax:
6. Explain the method st.add(x).
Appends element
x to the the end of the set st.
7. Explain the method st.pop().
Removes and returns the final element of the set
st.
8. Explain the method st1.union(set2)
Returns a new set containing all unique elements from both sets.
9. Explain the method st1.intersection(set2).
Returns a new set containing all unique elements that are common to both sets.
10. Explain the method st1.difference(set2).
Returns a new set containing all the elements that are in first set
st1 but not in the second set st2.
11. Provide 3 methods to create sets.
- In-Built Set Function:
set(<iterable>) - Set Comprehension:
{c for c in <sequence>} - Curly Braces:
{1, 2, 3, 4, 5}
12. Provide the two methods to remove the elements from a set.
s.remove('el')s.discard('el')to avoid KeyException
13. How to perform shallow copy and deep copy for a set?
s1.copy()s1.deepcopy()*Need to import:from copy import deepcopy
14. Explain frozen sets and its elements in terms of mutability.
These sets are immutable but their elements can be mutable.
15. Explain the limitations of frozen sets operations.
Frozen sets are limited to non-mutating set operations such as
(&, |, -, ^).
Dictionaries
1. Describe dictionaries.
A dictionary is a collection of key-value pairs. Where each key is unique and associated with a specific value.
2. How to create a dictionary? Provide an example.
You can create a set by enclosing a comma-separated sequence of key:value pairs within a curly brackets{}.
Example: my_dict = {'name': 'John', 'age': 30, 'city': 'New York'}
3. Are dictionaries mutable?
Dictionaries are mutable, meaning you can add, modify, or remove key-value pairs after the dictionary is created.
4. Can two different keys have the same value?
Yes.
5. Can two different values have the same keys?
No. Each key within a dictionary must have a distinct value associated with it.
6. Why do the dictionary keys have to be unique?
If keys were not unique, the dictionary would not know which value to return when you request a specific key, leading to ambiguity and making the retrieval process of the values unreliable.
7. Explain the method dict.keys().
Returns a list containing all the keys of the dictionary
dict.
8. Explain the method dict.values().
Returns a list containing all the values of the dictionary
dict.
9. Explain the method dict.items().
Returns a list containing all the keys:value pairs of the dictionary
dict.
10. Explain the method dict.clear().
To clear all the items (key-value pairs) within the dictionary
dict.
11. How to add a new key:value pair in a dictionary?
dict['new_key'] = new_value