1 of 47

Welcome to Lecture 16:

Lists + Mutability + List Comprehension

2 of 47

Today’s Topics

  • Announcements
  • Talk about lists in python
  • List mutability
  • List comprehension
  • Practice Problems

3 of 47

Announcements

  • Complete the survey released on weekly blast 5
  • project 4 released, sign up for a presentation slot
  • quiz 2 walkthrough Wed 7/23 at warren 101b. slides will be released shortly before.

4 of 47

Review

  • Strings are iterable and indexable (immutable in Snap! and Python)
    • We can concatenate strings:
    • We can splice strings
      • S1 = “hello”
      • S1[1:3] → “el”

​

5 of 47

Review

  • ​

​

​

​

  • We can also use “for item in _” with strings in python

6 of 47

Lists

  • Lists are mutable in Snap! and Python
    • We can index, splice, and concatenate lists

7 of 47

Lists in Python

  • Lists can store elements of any data type.
  • A list can contain a mix of integers, strings, other lists, functions, etc.
  • In python, I denote a list as brackets and a comma between each element:

​

8 of 47

Lists in Python

  • Just like Snap!, lists are iterable, indexable, and mutable

​

9 of 47

Lists in Python

  • We can also have nested lists.
    • nested_list[row][col]

​

10 of 47

Lists in Python

  • We can find the len of lists my calling: len(list)

​

​

​

​

11 of 47

Lists in Python

  • We can find the item at a particular index of a list

​

​

​

​

12 of 47

Lists in Python

  • We can ADD items to a list by calling: list.append(item)

​

​

​

​

13 of 47

Lists in Python

  • We can REPLACE items in our list by using reassignment:
    • list[position] = item

​

​

​

​

14 of 47

Lists in Python

  • We can DELETE items in our list by calling: list.pop(position)
    • Pop always returns the item that was deleted

​

​

​

​

​

15 of 47

Lists in Python

  • We can INSERT items in our list by calling: list.insert(position, item)

​

​

​

​

​

16 of 47

Lists in Python

  • We can JOIN two lists together by calling: list1 + list2

​

​

​

​

​

​

17 of 47

Guess that Output!

18 of 47

Guess that Output!

​

​

​

​

A nested list!

19 of 47

Guess that Output!

20 of 47

Guess that Output!

​

​

​

​

​

Throws an error! We cannot index an integer. But, we can stringify an integer

21 of 47

Guess that Output!

  • But, we can stringify an integer
  • We are casting the data type of our integer variable to a string.
  • Once we do this, we can index - but only on the stringfied version
  • We can also int-ify a string of numbers.

22 of 47

Guess that Output!

23 of 47

Guess that Output!

24 of 47

Guess that Output!

25 of 47

Guess that Output!

26 of 47

Guess that Output!

27 of 47

Guess that Output!

28 of 47

Guess that Output!

29 of 47

Guess that Output!

30 of 47

Guess that Output!

31 of 47

Guess that Output!

So when we say word_list[i][:3], we are calling the first three characters in each string

If i == 1, then word_list[i] is “Victoria’s” and word_list[i][:3] is “Vic”

32 of 47

Write the function:

Write a Python function sum_of_squares that takes a list of numbers as input and returns the sum of the squares of each number in the list. For example, if the input list is [1, 2, 3, 4], then:

sum_of_squares([1, 2, 3, 4]) and it should return 30:

1*1 + 2*2 + 3*3 + 4*4 = 30.

33 of 47

Write the function:

34 of 47

List Comprehension

  • Concise ways to iterate over a list or string and perform functions or operations over the items

35 of 47

List Comprehension with Strings

  • ​

36 of 47

List Comprehension

Function

Variable

List

37 of 47

List Comprehension

  • We can also use it to filter out items

38 of 47

List Comprehension

Function

Variable

List

Condition

39 of 47

List Comprehension

  • Using If and Else in list comprehension

40 of 47

List Comprehension

Function 1

Variable

List

Condition

Function 2

41 of 47

What about ‘Combine’

  • It’s not built-into python, we have to import a library
  • Reduce is didatic
  • To USE combine, we call the reduce function: reduce(function, list)

42 of 47

Write the function:

Write a Python function filter_and_double_odd_numbers that takes a list of numbers as input and returns a new list containing only the odd numbers from the input list, each multiplied by 2. For example, if the input list is [1, 2, 3, 4, 5], the function should return [2, 6, 10].

​

43 of 47

Write the function:

44 of 47

Write the function:

45 of 47

Write the function:

Write a Python function capitalize_long_words that takes a list of strings as input and returns a new list where only the strings with more than 3 characters are capitalized. You MUST use list comprehension

For example, if the input list is ["apple", "cat", "banana", "dog"], the function should return:

["APPLE", "cat", "BANANA", "dog"].

To capitalize words, you can use the “upper” function. Example:

print(“string”.upper())

>>> “STRING”

46 of 47

Write the function:

47 of 47

Write the function: