[CreativeProgramming]Lecture9_Python Lists
[CreativeProgramming]Lecture9_Python Lists
Spring 2025
CUL1122 Lecture #09
Python Lists
for Solving Optimization Problems
Today
3
Python Lists
4
List Items
5
List Items
❖2) Changeable: Lists are flexible because you can edit, add, or remove
items even after they are created.
▪ When you append new items to a list, they are added to the end.
▪ To modify a specific list item, assign a new value to its index.
6
List Items
❖3) Allowing Duplicates: Since lists are indexed, they can contain
duplicate values, with each occupying a unique position.
7
Counting Items in a List
❖The len() function takes a list as an argument and returns the number
of elements in that list.
❖In fact, len() can be used to determine the number of elements in any
iterable collection, including strings.
8
Using the range() Function
9
Enumerating List Items #1: Using for Loop with range()
❖To access each item in a list using range(len(list)), you can utilize a for
loop.
❖The loop variable, acting as an index, iterates over the range of indices
corresponding to the length of the list.
❖Let’s explore how to use a for loop with range(len(list)) to access each
item in a given list.
▪ 1) Iterating through the indices: The range(len(list)) function generates a
sequence of indices that correspond to the length of the provided list.
11
Enumerating List Items #2: Using enumerate()
12
Enumerating List Items #2: Using enumerate()
13
Adding Items to a List
14
Removing Items from a List
15
Checking for the Presence of a Specific Item
16
Checking the Quantity of a Specific Item
❖The count() method accepts a value as input and returns the number of
occurrences of that value in the list.
❖The syntax for count() is as follows: list.count(value).
Parameter Required Description Default
value Required The value to search for N/A
❖Similarly, the count(value, start, end) for strings returns the number of
times a value appears within the specified range of the given string.
Parameter Required Description Default
value Required The string to value to search for N/A
start Optional The position to start the search 0
end Optional The position to end the search End of the string
17
Checking the Quantity of a Specific Item
18
Finding the Index of a Specific Item
❖The index() method allows you to determine the index of a given item
within a list.
▪ In cases where duplicate items exist, it returns the index of the first occurrence
of the specified item.
▪ If the item is not found in the list, the method raises a ValueError.
❖The syntax for index() is as follows: list.index(item, start, end).
Parameter Required Description Default
item Required The item that you want to get the index N/A
start Optional The index to start the search 0
end Optional The index to end the search The end of the list
19
Finding the Index of a Specific Item
❖The index() method returns the index of the specified item in the list.
❖You can restrict the search by specifying the start and end indices.
20
List Slicing in Python
22
Lab 9
Today
24
Exercise #1: Listing Items in a List
❖Create a script that enumerates Netflix Korean dramas from the year
2023 using the range() as well as enumerate() functions.
▪ First, use range(len(list)) to generate a range for accessing each item.
▪ Then, use the enumerate() function to produce an enumerate object for
accessing each item.
25
Exercise #2: Finding the Index of a Specific Item in a List
❖Create a script to locate dramas based on their genres and display their
titles and number of episodes if found.
▪ Set up lists as follows:
➢drama_titles = [‘The Glory’, ‘Queenmaker’, ‘Black Knight’, ‘D.P. 2’, ‘Mask Girl’]
➢drama_genres = [‘Crime’, ‘Drama’, ‘SF’, ‘Military’, ‘Thriller’]
➢drama_num_episodes = [16, 11, 6, 6, 7]
▪ Search for dramas in the genres of ‘SF’ and ‘Comedy’.
26
Exercise #3: Calculating Manual Statistics
❖Create a script that calculates the maximum and average values from a
list of integers.
▪ Write two functions to find the maximum and average of a list of integers.
▪ Ask the user for a start and end value, then create a list of integers in that range.
▪ Use your functions to calculate and display the results.
27
Exercise #4: Verifying Palindromes
28
Exercise #4: Verifying Palindromes
29
Exercise #4: Verifying Palindromes
▪ Create a reversed version of the string and compare characters at each index
throughout the length of the string.
Index 0 1 2 3 4 5 6 7 8 9 10 11
Original String a s a n t a a t n a s a
Reversed String a s a n t a a t n a s a
30
수고하셨습니다!
31