Beginner · Lists, tuples, dictionaries and sets · topic 14 of 50 · 25 min
List methods and slicing
Explanation
Methods: actions that belong to a list
A method is a function that belongs to a value. You call it with a dot after the value, like cart.append("rice"). Lists come with many useful methods, and most of them change the list itself.
cart = ["rice", "dal"]
cart.append("oil")
cart.insert(0, "salt")
print(cart)cart = ["rice", "dal"]
cart.extend(["sugar", "tea"])
print(cart)
cart.remove("dal")
last = cart.pop()
print(last, cart)index(value) tells you where a value first appears, and count(value) tells you how many times it appears. pop(i) with an index removes the item at that position.
votes = ["tea", "coffee", "tea", "lassi", "tea"]
print(votes.index("coffee"))
print(votes.count("tea"))Sorting: sort() versus sorted()
my_list.sort() sorts the list in place, which means it changes the list itself, and it returns None. sorted(my_list) leaves the original alone and gives you a new sorted list. Add reverse=True to sort from biggest to smallest. my_list.reverse() simply flips the order.
marks = [65, 91, 78]
ranked = sorted(marks, reverse=True)
print(ranked)
print(marks)
marks.sort()
print(marks)
marks.reverse()
print(marks)Slicing: taking a part of a list
days = ["Mon", "Tue", "Wed", "Thu", "Fri"]
print(days[1:3])
print(days[:2])
print(days[-2:])
print(days[::-1])The alias mistake: b = a does not copy
a = [1, 2, 3]
b = a
b.append(4)
print(a)
c = a.copy()
c.append(5)
print(a)
print(c)A list of lists: a marks table
table = [
["Asha", 78, 85],
["Ravi", 92, 70],
]
print(table[1][0])
for row in table:
print(row[0], "total", row[1] + row[2])Watch the lesson
List methods and slicing · 1/7
In this lesson you will learn the methods that change a list, and how to take a slice of a list.
Prefer a video by a teacher? Find free videos on “python list methods append sort slicing tutorial” on YouTube ↗
In real life: A kirana shop's shopping list
Sunita keeps the list of things to buy from the wholesale market on her phone. During the day she adds items, removes what a neighbour already gave her, and at night she sorts the list so it is easy to read at the market. A copy of the list goes to her son, and his changes must not spoil hers.
to_buy = ["rice", "oil"]
to_buy.append("sugar")
to_buy.extend(["dal", "tea"])
to_buy.remove("oil")
to_buy.sort()
print(to_buy)
son_list = to_buy.copy()
son_list.append("biscuits")
print(len(to_buy), len(son_list))Practice (0/3 done)
Write a function add_item(cart, item) that adds item to the END of the list cart and then returns cart.
Write a function top_three(marks) that returns a new list of the three highest marks, biggest first. Do not change the original list. Example: top_three([55, 90, 72, 88, 61]) returns [90, 88, 72].
A marks table is a list of rows, and each row is a list of one student's marks. Write a function student_totals(table) that returns a list with the total of each row. Example: student_totals([[80, 75, 90], [60, 70, 65]]) returns [245, 195].
Quick quiz
Read more (free)
Pass the quiz (60% or more) or mark this topic as done when you are ready.
Your progress is saved on this device only.