0x04 Python - More Data Structures Set, Dictionary
0x04 Python - More Data Structures Set, Dictionary
01 02
Overview Learning
topics Objectives
03 04
Quiz hands on lab
questions practice
01
OVERVIEW topics
Topics
What are sets and how to use them
https://t.me/alx_2023
python
Programming
Topics
set is a built-in data type that is useful when you want to store a collection of
items where each item appears only once and the order of the items doesn't
matter.
You can also create a set from a list using the set() constructor:
my_list = [1, 2, 2, 3, 4, 4, 5]
my_set = set(my_list)
the most common methods of set and how to use them
remove(element): Removes the specified element from the set. Raises an error if
the element is not found.
my_set = {1, 2, 3}
my_set.remove(2)
print(my_set) # Output: {1, 3}
pop(): Removes and returns an arbitrary element from the set. Raises an error if
the set is empty.
my_set = {1, 2, 3}
popped_element = my_set.pop()
print(popped_element) # Output: (some element)
union(other_set) or | : Returns a new set containing all the unique elements from
both sets.
set1 = {1, 2, 3}
set2 = {3, 4, 5}
union_set = set1.union(set2)
# OR
union_set = set1 | set2
For loop:
my_set = {1, 2, 3, 4, 5}
key is a unique identifier that is used to associate a specific value with it.
Adding and Modifying Items: You can add or modify items in a dictionary by
assigning a value to a key.
my_dict["name"] = "jack" # Modifying an existing item
my_dict["color"] = "red" # Adding a new item
What is a key in a dictionary
Removing Items: You can remove items from a dictionary using the del keyword
or the pop() method.
Checking Membership: You can check if a key exists in a dictionary using the in
keyword.
if "key1" in my_dict:
print("Key 'key1' exists in the dictionary")
Dictionaries have several useful methods, including keys(), values(), items(), get(),
clear(), copy(), update(), and more.
How to iterate over a dictionary
A lambda function in Python is a small, anonymous, and inline function that can
have any number of arguments, but can only have one expression. It is sometimes
referred to as an "anonymous function" because it doesn't require a named
function definition using the def keyword.
lambda arguments: expression
add = lambda x, y: x + y
result = add(5, 3)
print(result) # Output: 8
#equal to
def sum(x,y):
return x + y
What are the map, reduce and filter functions
numbers = [1, 2, 3, 4, 5]
squared = map(lambda x: x ** 2, numbers)
squared_list = list(squared)
print(squared_list) # Output: [1, 4, 9, 16, 25]
What are the map, reduce and filter functions
numbers = [1, 2, 3, 4, 5, 6]
evens = filter(lambda x: x % 2 == 0, numbers)
evens_list = list(evens)
print(evens_list) # Output: [2, 4, 6]
What are the map, reduce and filter functions
numbers = [1, 2, 3, 4, 5]
product = reduce(lambda x, y: x * y, numbers)
print(product) # Output: 120 (1 * 2 * 3 * 4 * 5)
04
Hands on lab Practice
Have a Question
Leave a Comment!
Subscribe
To stay updated with latest
videos
Share
To let the others know more
Thanks