List Tuple Dictionary Set
ordered ordered ordered unordered
mutable if created with set
changeable (mutable) unchangeable (immutable) changeable immutable if created
with frozenset
allows duplicates allows duplicates does not allows duplicates does not allows duplicates
List items enclosed within [ ] Tuple items enclosed within ( ) Dictionary items enclosed within { } Set items enclosed within { }
Tuple can be created Dictionary can be created
List can be created using list() Set can be created using set()
using tuple() using dict()
List Functions:
1. append 2. extend 3. insert
4. remove >> used to remove specific item by using its value
5. pop >> used to remove specific item by using its index
6. clear >> To remove all items Built in Functions
7. index 1. sorted
8. count 2. reversed
9. copy 3. min
10. sort 4. max
11. reverse >> [::-1] 5. sum
Tuple Functions:
Tuple Functions Built in Functions
1. index 2. count 1. sorted 2. reversed 3. Min 4. max 5. sum
Set Functions:
1. Immutable FrozenSet
1. add 2. Update 3. Remove 4. discard 2. Unordered
5. pop 6. clear >> set() 3. Duplicates are not allowed
7. union 4. We can only add immutable items
8. intersection 5. Enclosed by frozenset()
9. intersection_update 6. Initialize with frozenset()
10. difference >> unique values from first set 7. Indexing and slicing is not applicable
11. difference_update
FrozenSet Functions
12. symmetric_difference >> unique values from both sets
1. union 2. Intersection 3. difference
13. symmetric_difference_update
4. symmetric_difference 5. issuperset 6. Issubset
14. issuperset 15. Issubset 16. isdisjoint
7. isdisjoint
Dict Functions:
Dict Functions:
1. get 2. Keys 3. Values 4. Items 5. Update 6. Pop 7. popitem >> It will delete last inserted item 8. clear >> {}
9. fromkeys() 10. setdefault 11. copy()
A list comprehension is the correct answer:
>>> list = [i for i in range(10) if i % 2 == 0]
print(list)
# dictionary comprehension example
square_dict = {num: num*num for num in range(1, 11)}
print(square_dict)
#item price in dollars
old_price = {'milk': 1.02, 'coffee': 2.5, 'bread': 2.5}
dollar_to_pound = 0.76
new_price = {item: value*dollar_to_pound for (item, value) in old_price.items()}
print(new_price)