Topics Covered
In the last post, we learned about Python's String data type. In this post, we'll learn about Python List data types and the most popular methods used on Python Lists.
Optional: Creating chapter-wise Python files
Let's create a directory that will house fresh Python files with code samples from each post in order to keep track of what we've learned so far from each one. For the purpose of keeping all learning in one location, this step is optional but is advised to set up.
Python Lists are mutable sequences and used to store collections of homogeneous items.
Lists may be constructed in several ways, lets have a look to some of the most common methods. Write below code snippet in learn_lists.py and run the script.
Python lists can be constructed using square brackets, by separating each item with commas.
# list using square brackets
my_list = ["apple", "orange", "banana"]
print(my_list)
print(type(my_list))
['apple', 'orange', 'banana']
<class 'list'>
Python lists can be created using list comprehension as shown below.
# list using list comprehension
my_list = [i*2 for i in range(0,10)]
print(my_list)
print(type(my_list))
[0, 2, 4, 6, 8, 10, 12, 14, 16, 18]
<class 'list'>
Python lists can be created using list()
type constructor.
# list using type constructor
my_list_1 = list((1,2,3,4,5))
my_list_2 = list(("apple", "orange", "banana"))
print(my_list_1)
print(type(my_list_1))
print(my_list_2)
print(type(my_list_2))
[1, 2, 3, 4, 5]
<class 'list'>
['apple', 'orange', 'banana']
<class 'list'>
Let's learn some of the most important Python list methods with examples.
append()
Exampleappend()
method appends object to the end of the list.
# list append() method
my_list = ["red", "blue", "orange"]
my_list.append("blue")
print(my_list)
['red', 'blue', 'orange', 'blue']
clear()
ExamplePython list clear()
method removes all items from list.
# list clear() method
my_list = ["red", "blue", "orange"]
my_list.clear()
print(my_list)
[]
copy()
ExamplePython list copy()
method returns a shallow copy of the list.
# list copy() method
my_list = ["red", "blue", "orange"]
my_list_1 = my_list.copy()
print(my_list)
print(my_list_1)
['red', 'blue', 'orange']
['red', 'blue', 'orange']
extend()
ExamplePython list extend()
method extends the list by appending the elements from the iterable.
# list extend() method
my_list_1 = [1,2,3,4,5]
print(my_list_1)
my_list_2 = ["one","two","three","four"]
print(my_list_2)
my_list_2.extend(my_list_1)
print(my_list_2)
[1, 2, 3, 4, 5]
['one', 'two', 'three', 'four']
['one', 'two', 'three', 'four', 1, 2, 3, 4, 5]
index()
ExamplePython list index()
method returns the first index of value.
# list index() method
my_list = ["red", "blue", "orange", "blue","red", "green"]
idx_red = my_list.index("red")
print(idx_red)
idx_blue = my_list.index("blue")
print(idx_blue)
idx_green = my_list.index("green")
print(idx_green)
0
1
5
pop()
ExamplePython list pop()
method removes and returns the item at given index, if index is not provided than last item is removed.
# list pop() method
my_list = ["apple", "mango", "orange", "banana", "rose"]
print(my_list)
# pop() without index
last_item = my_list.pop()
print(my_list)
print(last_item)
# pop() with index
my_list_1 = [10,20,30,40,50,60]
removed_item = my_list_1.pop(2)
print(my_list_1)
print(removed_item)
['apple', 'mango', 'orange', 'banana', 'rose']
['apple', 'mango', 'orange', 'banana']
rose
[10, 20, 40, 50, 60]
30
remove()
ExamplePython list remove()
method removes first occurrence of a given value.
# list remove() method
my_list = ["apple", "mango", "orange", "mango", "banana", "rose"]
print(my_list)
my_list.remove("mango")
print(my_list)
['apple', 'mango', 'orange', 'mango', 'banana', 'rose']
['apple', 'orange', 'mango', 'banana', 'rose']
reverse()
ExamplePython list reverse()
method reverse the list in place.
# list reverse() method
my_list = [1,2,3,4,5,6,7]
print(my_list)
my_list.reverse()
print(my_list)
[1, 2, 3, 4, 5, 6, 7]
[7, 6, 5, 4, 3, 2, 1]
sort()
ExamplePython list sort()
method sorts the list in ascending order and if reverse
flag is set than sorts in descending order.
# list sort() method
my_list = [6,4,1,5,3,7,2]
print(my_list)
# sort in ascending order
my_list.sort()
print(my_list)
# sort in descending order
my_list.sort(reverse=True)
print(my_list)
[6, 4, 1, 5, 3, 7, 2]
[1, 2, 3, 4, 5, 6, 7]
[7, 6, 5, 4, 3, 2, 1]