0% found this document useful (0 votes)
21 views

0x04 Python - More Data Structures Set, Dictionary

The document discusses Python data structures sets and dictionaries. It explains how to create, iterate and manipulate sets and dictionaries as well as when to use each. It also covers lambda functions, map, filter and reduce.

Uploaded by

ELOTHMANI93
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views

0x04 Python - More Data Structures Set, Dictionary

The document discusses Python data structures sets and dictionaries. It explains how to create, iterate and manipulate sets and dictionaries as well as when to use each. It also covers lambda functions, map, filter and reduce.

Uploaded by

ELOTHMANI93
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 26

ALX LESSON

0x04 Python - More


Data Structures
Set, Dictionary
python - Programming
TABLE OF CONTENTS

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

What are the most common methods of set and how to


use them

When to use sets versus lists

How to iterate into a set

python What are dictionaries and how to use them

Programming When to use dictionaries versus lists or sets

Topics What is a key in a dictionary

How to iterate over a dictionary

What is a lambda function

What are the map, reduce and filter functions


Slides On Telegram

https://t.me/alx_2023

python
Programming
Topics

Slides exist on telegram


@alx_2023ch
02
Learning Objectives
What are sets and how to use them

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.

Creating a Set: You can create a set by enclosing a comma-separated list of


elements within curly braces {}
my_set = {1, 2, 3, 4, 5}

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

add(element): Adds an element to the set.


my_set = {1, 2, 3}
my_set.add(4)
print(my_set) # Output: {1, 2, 3, 4}

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}

discard(element): Removes the specified element from the set if it is present,


without raising an error if the element is not found.
my_set.discard(2)
print(my_set) # Output: {1, 3}
the most common methods of set and how to use them

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)

clear(): Removes all elements from the set, making it empty.


my_set = {1, 2, 3}
my_set.clear()
print(my_set) # Output: set()

copy(): Returns a shallow copy of the set.


my_set = {1, 2, 3}
copied_set = my_set.copy()
the most common methods of set and how to use them

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

intersection(other_set) or & : Returns a new set containing elements that are


present in both sets.
intersection_set = set1.intersection(set2)
# OR
intersection_set = set1 & set2
the most common methods of set and how to use them

difference(other_set) or - : Returns a new set containing elements that are in the


first set but not in the second set.
difference_set = set1.difference(set2)
# OR
difference_set = set1 - set2

symmetric_difference(other_set) or ^ : Returns a new set containing elements


that are in either of the sets, but not in both.
symmetric_difference_set = set1.symmetric_difference(set2)
# OR
symmetric_difference_set = set1 ^ set2
When to use sets versus lists
How to iterate into a set

For loop:

my_set = {1, 2, 3, 4, 5}

for element in my_set:


print(element)
What are dictionaries and how to use them

dictionary is a built-in data structure that stores a collection of key-value pairs.


Each key in a dictionary is unique and maps to a specific value. Dictionaries are
useful when you want to associate data with specific labels (keys) for efficient
and quick access.

Creating a Dictionary: You can create a dictionary by enclosing a comma-


separated list of key-value pairs within curly braces {}.
my_dict = {"key1": "value1", "key2": "value2", "key3": "value3"}

You can also create a dictionary using the dict() constructor:


my_dict = dict(key1="value1", key2="value2", key3="value3")
What is a key in a dictionary

key is a unique identifier that is used to associate a specific value with it.

my_dict = {"name": "John", "age": 30, "city": "New York"}

# Accessing values using keys


name = my_dict["name"]
age = my_dict["age"]
city = my_dict["city"]

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.

del my_dict["key2"] # Deleting an item by key


removed_value = my_dict.pop("key3") # Removing an item and returning its
value

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

# Iterating over keys


for key in my_dict:
print(key)

# Iterating over values


for value in my_dict.values():
print(value)

# Iterating over key-value pairs


for key, value in my_dict.items():
print(key, value)
When to use dictionaries versus lists or sets
What is a lambda function

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

map(function, sequence): The map() function applies a given function to each


element in a sequence (such as a list) and returns a new sequence containing the
results.

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

filter(function, sequence): The filter() function filters elements from a sequence


based on whether they satisfy a given function (predicate). It returns a new
sequence containing only the elements that pass the filter.

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

reduce(function, sequence): The reduce() function, previously part of the functools


module (in Python 3.0+), successively applies a given function to the elements of
a sequence, reducing it to a single accumulated result.

from functools import reduce

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

You might also like