8. Removing Items - List
Remove Elements in a List:
myList1 = ['a','b','c']
myList1.remove('a')
myList1.remove('b')
myList1.remove('c')
# Output:
[]
Note* : Even though there are no elements
in the list, it still takes up memory, we
can still add elements!
9. Exercise
# empty list
my_list = []
# list of integers
my_list = [1, 2, 3]
# list with mixed data types
my_list = [1, "Hello", 3.4]
# nested list (array inside of an array)
my_list = ["mouse", [8, 4, 6], ['a']]
* Question: How can we access "mouse" or ['a']? *
12. Loops in flow chart
• Given a list list_sample = [4, 3, 2, 5, 8], print all the even numbers
13. Loops in flow chart
Start
Declare list_sample [4, 3, 2, 5, 8]
Declare a variable len_list that stored the length of the list
14. Loops in flow chart
Start
Declare list_sample [4, 3, 2, 5, 8]
Declare a variable len_list that stored the length of the list
15. Loops in flow chart
Start
Declare list_sample [4, 3, 2, 5, 8]
Declare a number a=0
Declare a variable len_list that stores the length of the list
If a >
len_list
Stop a = a+1
value =
list_sample[a]
If value is
a even
number
Print value
True False
False True
16. Loops in flow chart
• Given a list list_sample = [4, 3, 2, 5, 8], print all the even numbers
17. Loops in flow chart - while
• Given a list list_sample = [4, 3, 2, 5, 8], print all the even numbers
18. Loops in flow chart - while
• Given a list list_sample = [4, 3, 2, 5, 8], print all the even numbers
Declare a number a=0
If a >
len_list
Stop a = a+1
value =
list_sample[a]
If value is
a even
number
Print value
True False
False True
20. Exercise: Design a chat bot - flowchart
• list_happy_words = [“joyful”, “happy”, “amazing”, “beautiful”]
• list_sad_words = [“sad”, “tears”, “crying”, “depressed”]
• list_games = [“tetris”, “flappy bird”, “snake”]
• Ask the user to enter a word that describes their feeling
• If the feeling they enter is one of the list_happy_words, print “Glad you are
happy” and exit.
• If the feeling they enter is one of the list_sad_words, ask the user to play
one of the games from the list list_games.
• Ask the user again how they are feeling and continue till the list of games is
exhausted.
• If you already suggested all the games and the user is still unhappy, exit.
21. Exercise: Design a chat bot - code
Start
Declare the 3 lists
Ask the user to input a word describing their feeling, save the string
Check if
word is a
part of
happy list
Check if
word is a
part of
sad list
True
Stop
Print
sentence
False
False True
Print
sentence
Print
game
But what if you exhaust the list of
games?
This flowchart does not check for it
22. Exercise: Design a chat bot - code
Start
Declare the 3 lists
Ask the user to input a word describing their feeling, save the string
Check if
word is a
part of
happy list
Check if
word is a
part of
sad list
True
Stop
Print
sentence
False
False True
Print
sentence
Print
game
Check if
your
games
list is
over
False