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

08 - Session - Data Structures

This document discusses Python data structures like lists and sets. It provides examples of common list methods like len(), max(), min(), sum(), append(), insert(), remove(), pop(), reverse(), and sort. It also introduces sets, explaining that they are unordered, mutable collections that cannot contain duplicates. Set operations like union(), intersection(), difference(), and issubset() are demonstrated. Finally, programming challenges are provided involving lists, sets, extracting data, and removing duplicates.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

08 - Session - Data Structures

This document discusses Python data structures like lists and sets. It provides examples of common list methods like len(), max(), min(), sum(), append(), insert(), remove(), pop(), reverse(), and sort. It also introduces sets, explaining that they are unordered, mutable collections that cannot contain duplicates. Set operations like union(), intersection(), difference(), and issubset() are demonstrated. Finally, programming challenges are provided involving lists, sets, extracting data, and removing duplicates.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

CODING PRINCIPLES

Session 8

Data structures

Pablo Monfort Instituto de Empresa


Python simple data structures

2
Summary

▪ Lists
▪ Sets

3
3
Sequences

num_list = [1, 3, 5, 7, 9]

print(len(num_list)) # Check the length


print(max(num_list)) # Check the max
print(min(num_list)) # Check the min
print(sum(num_list)) # Check the sum
print(sum(num_list)/len(num_list)) # Check the mean
4
Lists are sequences

• len() function will return the length of a sequence

• The in operator checks if an element is a member of a


sequence:
- If the element is a member, the result is true else it is false

list1 = ["Anne", "David", "Joanna", "Andrew", "William"]

"Anne" in list1 # check if a value is in the list


"David" not in list1 # check if a value is not in a list
list1.count("Andrew") # count the occurences of an object within a list
list1.reverse() # reverse the list
list1.sort() # sort the list

5
Lists are sequences
list1 = ["Anne", "David", "Joanna", "Andrew", "William"]

list1[0] "Anne"
list1[2] "Joanna"
list1[-1] "William"
list1[-3] "Joanna"
list1[1:3] ["David", "Joanna"]
list1[0:6:2] ["Anne","Joanna","William"]
list1[:4] ["Anne", "David", "Joanna", "Andrew"]
list1[3:] ["Andrew","William"]
list1[4:2:-1] ["William","Andrew"]
list1[:] ["Anne", "David", "Joanna", "Andrew", "William"]
list1[::-1] ["William","Andrew","Joanna","David","Anne"]

list1.remove("Andrew") ["Anne","David","Joanna","William"]
list1.pop(2) ["Anne","David","William"]
list1.append("Fernando") ["Anne","David","William","Fernando"]
list1.insert(3,"Sarah") ["Anne","David","William","Sarah","Fernando"]
list1.pop() ["Anne","David","William","Sarah"]
6
Lists: Adding new items

• append(value) adds at the end of the list

• insert(index,value) allows you to insert a value at a given index

7
Lists: Removing items

• listName.remove(value)
• listName.pop()
• listName.pop(index)

8
Lists: sort() vs. sorted()

the list does not change


(a reassignment is
necessary to modify the
list)

the list changes


(the list is modified
automatically) *

* all the commands such as object.command() modify the object automatically 9


Programming Challenges
Programming challenge DS.1
Write a Python program which multiplies all the items in a list

Programming challenge DS.2

Write a Python program to remove duplicated values from a list


Input: [1,2,3,2,2,2,1,5,5,7,8,9] Output: [1,2,3,5,7,8,9]

Programming challenge DS.3

Write a Python program to get the total income for your top 20% of
clients once you receive a list with all the sales.
Input: [140, 200, 160, 300, 300, 20, 5, 10, 1000, 100] Output: 1300

B. Rewrite the previous script using a function with 2 arguments:


sales and percentage of top sales to consider

10
Programming Challenges

Programming challenge DS.4

Write a Python program to check if there are some negative value in a


given list.
Expected output: There is (or there is not) negative values.

Programming challenge DS.5


From a list with 10 emails, extract the names of the companies.

Input: ["[email protected]", “[email protected]",


[email protected]", “[email protected]", “[email protected]"…]
Output: "canalyticals", "ie", "vodafone" and "tmobile"

Tip: check the following command


vble = "recoletos street - Madrid - Spain"
vble.split("street")

11
Summary

▪ Lists
▪ Sets

12
12
Sets – Non duplicative unordered collections
Sets:
- are unordered
- are faster than lists, tuples and strings
- cannot contain duplicates
- are mutable
- are created with {}

13
Sets – Operations

14
Sets – Math Operations

my_list = [1, 1, 1, 3, 5, 7, 9]
my_set1 = set(my_list)
my_set2 = {1,9,20,40,"Pablo"}

len(my_set1)
my_set1.union(my_set2)
my_set1.intersection(my_set2)
my_set1.difference(my_set2)
my_set1.issubset(my_set2)
15
Sets – Math Operations

16
Programming Challenges

Programming challenge DS.6

Write a Python program to find maximum and the minimum value in a


set.

Programming challenge DS.7

Write a Python program to remove a concrete item from a set if it is


present in the set.

Programming challenge DS.8

Write a Python program to do (A U B) ∩ C and the result has to be


composed by only 2 elements

17
Programming Challenges

Programming challenge DS.9

Create a program to erase duplicated values from a list

Programming challenge DS.10

You work in Telefonica and the marketing department sends a list with
all the clients IDs (around 1M). How can you check if it is correct or if
you have received duplicated IDs?

IDS = ["899887X", "546522E", "223321F"…]

18
Session Wrap-up

19
Glossary
in, not in li = […] s = {…} dict = {…}
min, max, sum list set dict

.count() .add() .keys()


.reverse() .clear() .values()
.sort() .union() .items()
.pop() .intersection() .update()
.append() .difference()
.copy() .issubset()
.title()
.join() &, |, ^, -, <=, >=
.remove()

del
sorted

Extra content: No studied


20

You might also like