Manipulating lists for fun and profit
You may be familiar with adding individual data elements to a list by using the .append()
method. However, if you want to combine a list with another array type (list, set, tuple), you
can use the .extend() method on the list.
You can also use the .index() method to find the position of an item in a list. You can then use that position to remove the item with the .pop() method.
In this exercise, you'll practice using all these methods!
Latihan ini adalah bagian dari kursus
Data Types in Python
Petunjuk latihan
- Create a list called
baby_nameswith the names'Ximena','Aliza','Ayden', and'Calvin'. - Use the
.extend()method onbaby_namesto add'Rowen'and'Sandeep'and print the list. - Use the
.index()method to find the position of'Rowen'in the list. Save the result asposition. - Use the
.pop()method withpositionto remove'Rowen'from the list.
Latihan interaktif praktis
Cobalah latihan ini dengan menyelesaikan kode contoh berikut.
# Create a list containing the names: baby_names
baby_names = ____
# Extend baby_names with 'Rowen' and 'Sandeep'
____
# Find the position of 'Rowen': position
position = ____
# Remove 'Rowen' from baby_names
____
# Print baby_names
print(baby_names)