Lecture 31 – Python Lists
and List Manipulation-III
Programming Fundamentals
Previous Lecture Review
• Built-in List Functions
• len() Function
• The min and max Functions
• Copying Lists
Outline
• List Methods
• List append() Method
• List count() Method
• List extend() Method
• List index() Method
• List sort() Method
• List reverse() Method
List Methods
• Lists have numerous methods that allow you to add elements, remove elements,
change the ordering of elements, and so forth. We will look at a few of these
methods,1 which are listed below.
Sr.No. Function with Description
1 append(item): Adds item to the end of the list.
2 count(item): Returns the count of number of items passed as an argument.
3 extend(seq): Add all elements of a list to the another list.
4 index(obj): Returns the index of the first matched item.
5 sort(): Sort items in a list in ascending order.
6 reverse(): Reverse the order of items in the list.
List append() Method
• The append method is commonly used to add items to a list. The item that is
passed as an argument is appended to the end of the list’s existing elements.
name_list = []
again = 'y
Program Output:
while again == 'y’:
Enter a name: Quaid
name = input('Enter a name: ‘) Do you want to add another name?
name_list.append(name) y = yes, anything else = no: y
print('Do you want to add another name?’) Enter a name: Azam
again = input('y = yes, anything else = no: ') Do you want to add another name?
print() y = yes, anything else = no: y
print('Here are the names you entered.') Enter a name: Khan
for name in name_list: Do you want to add another name?
print(name) y = yes, anything else = no: n
Here are the names you entered.
Quaid
Azam
Khan
List count() Method
• The count() method returns the number of occurrences of an element in a list. In
simple terms, count() method counts how many times an element has occurred
in a list and returns it.
• Example
vowels = ['a', 'e', 'i', 'o', 'i', 'u’]
count = vowels.count(‘i’)
print('The count of i is:', count)
count = vowels.count('p’)
print('The count of p is:', count)
>> The count of i is: 2
>> The count of p is: 0
List extend() Method
• The extend() extends the list by adding all items of a list (passed as an argument)
to the end.
• Example
language = ['French', 'English', 'German']
language1 = ['Spanish', 'Portuguese']
language.extend(language1)
print('Language List: ', language)
>> Language List: ['French', 'English', 'German', 'Spanish', 'Portuguese']
List index() Method
• The index method returns the index of the first element in the list containing that
item. If the item is not found in the list, the method raises a ValueError exception.
food = ['Pizza', 'Burgers', 'Chips']
print('Here are the items in the food list:')
Program Output:
print(food) Here are the items in the food
item = input('Which item should I change? ') list:
item_index = food.index(item) ['Pizza', 'Burgers', 'Chips']
Which item should I change?
new_item = input('Enter the new value: ') Burgers
food[item_index] = new_item Enter the new value: Pickles
print('Here is the revised list:') Here is the revised list:
print(food) ['Pizza', 'Pickles', 'Chips']
print('That item was not found in the list.')
List sort() Method
• The sort method rearranges the elements of a list so they appear in ascending order (from the
lowest value to the highest value). Here is an example:
vowels = ['e', 'a', 'u', 'o', 'i’]
vowels.sort()
print('Sorted list:', vowels)
>> Sorted list: ['a', 'e', 'i', 'o', 'u']
• Setting reverse=True sorts the list in the descending order.
vowels = ['e', 'a', 'u', 'o', 'i’]
vowels.sort(reverse=True)
print('Sorted list (in Descending):', vowels)
>> Sorted list (in Descending): ['u', 'o', 'i', 'e', 'a']
List reverse() Method
• The reverse method simply reverses the order of the items in the list. Here is an
example:
my_list = [1, 2, 3, 4, 5]
print('Original order:', my_list)
my_list.reverse()
print('Reversed:', my_list)
>> Original order: [1, 2, 3, 4, 5]
>> Reversed: [5, 4, 3, 2, 1]
References
• Think Python: How to Think like a Computer Scientist by Allen B. Downey, Publisher: O'Reilly
Media; 2 edition (December 28, 2015).
• Starting Out with Python by Tony Gaddis, Pearson; 4 edition (March 16, 2017)
• https://www.programiz.com/python-programming/list