05 Lists Advanced
05 Lists Advanced
SoftUni Team
Technical Trainers
Software University
https://softuni.bg
Table of Contents
1. List Comprehensions
2. List Methods
3. Advanced Functions
4. Additional List Manipulations
5. The set() function
2
Have a Question?
sli.do
#fund-python
3
List Comprehensions
What is Comprehension?
▪ Comprehensions provide us with a short way to
construct new sequences
▪ They allow sequences to be built from
other sequences
▪ They require less memory
▪ They have shorter syntax and better performance
5
Structure
▪ A list comprehension consists of the following parts:
▪ An input sequence
▪ A variable representing members of the input sequence
▪ An optional predicate expression
▪ An output expression producing elements in the output list
6
List Comprehensions
▪ Creating a list using the range function
Output Expression
7
List Comprehensions (2)
▪ Using if statement in a list comprehension
nums = [1, 2, 3, 4, 5, 6]
evens = [num for num in nums if num % 2 == 0]
# [2, 4, 6]
Optional Parameter
▪ Using if-else statement in a list comprehension
nums = [1, 2, 3, 4, 5, 6]
filtered = [True if x % 2 == 0 else False for x in nums]
# [False, True, False, True, False, True]
8
Problem: No Vowels
▪ Write a program that receives a text and removes all the
vowels from it
▪ Print the new text string after removing the vowels
▪ The vowels that should be considered are 'a', 'o', 'u', 'e', 'i'
ILovePython LvPythn
9
Solution: No Vowels
text = input()
vowels = ['a', 'u', 'e', 'i', 'o', 'A', 'U', 'E', 'I', 'O']
no_vowels = ''.join([x for x in text if x not in vowels])
print(no_vowels)
10
List Methods
Adding Elements
▪ Using the append() method
Add single element
my_list = [1, 2, 3] at the end
my_list.append(4) # [1, 2, 3, 4]
wow father mom wow shirt stats ['wow', 'mom', 'wow', 'stats']
wow Found palindrome 2 times
19
Solution: Palindrome Strings
20
Advanced Functions
Using Lambda Operators
sorted() Function
▪ Sorts the elements of a list in ascending order
numbers_list = [6, 2, 1, 4, 3, 5]
sorted_numbers = sorted(numbers_list)
# [1, 2, 3, 4, 5, 6]
22
Problem: Sorting Names
▪ Write a program that reads a single string with names
separated by comma and space ", "
▪ Sort the names by their length in descending order
▪ If 2 or more names have the same length, sort them in
ascending order (alphabetically)
▪ Print the resulting list Ali, Marry, Kim, Teddy, Monika, John
23
map() Function
▪ Use it to convert list of strings to list of integers
Returns int(x) for each
element x in the list
strings_list = ["1", "2", "3", "4"]
numbers_list = list(map(int, strings_list)) # [1, 2, 3, 4]
25
Problem: Even Numbers
▪ Write a program that reads a single string with numbers
separated by comma and space ", "
▪ Print the indices of all even numbers
3, 2, 1, 5, 8 [1, 4]
2, 4, 6, 9, 10 [0, 1, 2, 4]
26
Solution: Even Numbers
27
Problem: The Office
▪ Read the problem description here
1 2 3 4 2 1
Score 2/6. Employees are not happy!
3
2 3 2 1 3 3
Score: 3/6. Employees are happy!
4
28
Solution: The Office
29
Additional List Manipulations
Swapping List Elements
▪ You can use the following syntax to swap two or more
list elements
nums = [1, 2, 3]
nums[0], nums[1], nums[2] = nums[2], nums[0], nums[1]
# 1 swaps with 3
# 2 swaps with 1
# 3 swaps with 2
▪ The first element on the left swaps with the first on the
right etc.
31
Concatenating Lists
▪ You can use the "+" operator to join two lists
nums_list_1 = [1, 2, 3]
nums_list_2 = [4, 5, 6]
final_list = nums_list_1 + nums_list_2
print(final_list) # [1, 2, 3, 4, 5, 6]
32
The Set Function
▪ You can use the set() function to extract only the unique
elements from a list
numbers = [1, 2, 2, 3, 1, 4, 5, 4]
unique_numbers = list(set(numbers)) # [1, 2, 3, 4, 5]
33
Live Exercises
Summary
▪▪ We
… learned:
▪ ▪…Some additional methods that can be
▪ …used with lists
▪ Some basic lambda functionality
▪ How to swap list elements
35
Questions?
SoftUni Diamond Partners
Educational Partners
38
Trainings @ Software University (SoftUni)
▪ Software University – High-Quality Education,
Profession and Job for Software Developers
▪ softuni.bg, softuni.org
▪ Software University Foundation
▪ softuni.foundation
▪ Software University @ Facebook
▪ facebook.com/SoftwareUniversity
▪ Software University Forums
▪ forum.softuni.bg
39
License
40