Chapter 6 - List
Chapter 6 - List
Lists
07/19/23 1
Python Lists
Example
thislist = ["apple", "banana", "cherry", "apple", "cherry"]
print(thislist)
List Length
Example:
• List items are indexed and you can access them by referring
to the index number:
Example: Print the second item of the list:
thislist = ["apple", "banana", "cherry"]
print(thislist[1])
• Negative indexing means start from the end
-1 refers to the last item, -2 refers to the second last item etc.
Example: Print the last item of the list:
thislist = ["apple", "banana", "cherry"]
print(thislist[-1])
Range of Indexes
thislist = ["apple", "banana", "cherry", "orange", "kiwi", "melon", "mango"]
print(thislist[-4:-1])
Example
Change the values "banana" and "cherry" with the values "blackcurrant"
and "watermelon":
07/19/23 14
Change a Range of Item Values
• If you insert more items than you replace, the new items will be
inserted where you specified, and the remaining items will move
accordingly:
Example
Change the second value by replacing it with two new values:
thislist = ["apple", "banana", "cherry"]
thislist[1:2] = ["blackcurrant", "watermelon"]
print(thislist)
Note: The length of the list will change when the number of items
inserted does not match the number of items replaced.
07/19/23 15
Change a Range of Item Values
• If you insert less items than you replace, the new items will be inserted
where you specified, and the remaining items will move accordingly:
Example
Change the second and third value by replacing it with one value:
Append Items
•To add an item to the end of the list, use the append() method:
Example
Using the append() method to append an item:
Insert Items
•To insert a list item at a specified index, use the insert() method.
Example
Note: As a result of the example above, the list will now contain 5 items.
Add List Items
Extend List
•To append elements from another list to the current list, use the extend()
method.
Example
Add the elements of tropical to thislist:
thislist = ["apple", "banana", "cherry"]
tropical = ["mango", "pineapple", "papaya"]
thislist.extend(tropical)
print(thislist)
Example
Add elements of a tuple to a list:
• If you do not specify the index, the pop() method removes the last item.
• List comprehension offers a shorter syntax when you want to create a new
list based on the values of an existing list.
• Comprehension syntax:
newlist = [expression for item in list if condition == True]
Example: Based on a list of fruits, you want a new list, containing only the
fruits with the letter "a" in the name.
fruits = ["apple", "banana", "cherry", "kiwi", "mango"]
•The condition if x != "apple" will return True for all elements other than
"apple", making the new list contain all fruits except "apple".
Example:
li = [3, 6, 2, 7]
l1=[elem*2 for elem in li]
print(l1)
List Comprehension
• The iterable can be any iterable object, like a list, tuple, set etc.
Example: You can use the range() function to create an iterable:
newlist = [x for x in range(10)]
Expression
•The expression is the current item in the iteration, but it is also the outcome,
which you can manipulate before it ends up like a list item in the new list:
Example: Set the values in the new list to upper case:
newlist = [x.upper() for x in fruits]
• List objects have a sort() method that will sort the list alphanumerically
and numerically, ascending, by default:
• Sort List Alphanumerically
Example:
thislist = ["orange", "mango", "kiwi", "pineapple", "banana"]
thislist.sort()
print(thislist)
• Sort the list numerically:
Example:
thislist = [100, 50, 65, 82, 23]
thislist.sort()
print(thislist)
Sort Lists
• Sort Descending :To sort descending, use the keyword argument reverse =
True
Example 1:
thislist = ["orange", "mango", "kiwi", "pineapple", "banana"]
thislist.sort(reverse = True)
print(thislist)
Example 2:
thislist = [100, 50, 65, 82, 23]
thislist.sort(reverse = True)
print(thislist)
Sort Lists
Reverse Order
•What if you want to reverse the order of a list, regardless of the alphabet?
The reverse() method reverses the current sorting order of the elements.
Example: Reverse the order of the list items:
Another way to join two lists is by appending all the items from list2 into list1,
one by one:
Or you can use the extend() method, which purpose is to add elements from one
list to another list:
Example: Use the extend() method to add list2 at the end of list1:
• There are four collection data types in the Python programming language:
• List is a collection which is ordered and changeable. Allows duplicate
members.
• Tuple is a collection which is ordered and unchangeable. Allows duplicate
members.
• Set is a collection which is unordered and unindexed. No duplicate
members.
• Dictionary is a collection which is ordered* and changeable. No duplicate
members.
• *As of Python version 3.7, dictionaries are ordered. In Python 3.6 and
earlier, dictionaries are unordered.
• When choosing a collection type, it is useful to understand the properties
of that type. Choosing the right type for a particular data set could mean
retention of meaning, and, it could mean an increase in efficiency or
security.