Python List Data Type

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.

  • Open VScode and create new folder python-with-gcptutorials
  • Inside python-with-gcptutorials create file learn_lists.py

vscode-new-file


Lists in Python

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.

  • Using square brackets

    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))
       
    Output
       
    ['apple', 'orange', 'banana']
    <class 'list'>
       
  • Using a list comprehension

    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))
       
    Output
       
    [0, 2, 4, 6, 8, 10, 12, 14, 16, 18]
    <class 'list'>
       
  • Using the type constructor

    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))
       
    Output
       
    [1, 2, 3, 4, 5]
    <class 'list'>
    ['apple', 'orange', 'banana']
    <class 'list'>
       

List Methods in Python

Let's learn some of the most important Python list methods with examples.

Python List append() Example

append() method appends object to the end of the list.

   
# list append() method
my_list = ["red", "blue", "orange"]
my_list.append("blue")

print(my_list)
 
Output
   
['red', 'blue', 'orange', 'blue']
 

Python List clear() Example

Python list clear() method removes all items from list.

   
# list clear() method
my_list = ["red", "blue", "orange"]
my_list.clear()

print(my_list)
  
 
Output
   
[]
 

Python List copy() Example

Python 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)  
  
 
Output
   
['red', 'blue', 'orange']
['red', 'blue', 'orange']
 

Python List extend() Example

Python 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)  
 
Output
   
[1, 2, 3, 4, 5]
['one', 'two', 'three', 'four']
['one', 'two', 'three', 'four', 1, 2, 3, 4, 5]
 

Python List index() Example

Python 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) 
 
Output
   
0
1
5
 

Python List pop() Example

Python 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)
 
Output
   
['apple', 'mango', 'orange', 'banana', 'rose']
['apple', 'mango', 'orange', 'banana']
rose
[10, 20, 40, 50, 60]
30
 

Python List remove() Example

Python 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)
  
 
Output
   
['apple', 'mango', 'orange', 'mango', 'banana', 'rose']
['apple', 'orange', 'mango', 'banana', 'rose']
 

Python List reverse() Example

Python 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)
  
 
Output
   
[1, 2, 3, 4, 5, 6, 7]
[7, 6, 5, 4, 3, 2, 1]
 

Python List sort() Example

Python 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)
  
 
Output
   
[6, 4, 1, 5, 3, 7, 2]
[1, 2, 3, 4, 5, 6, 7]
[7, 6, 5, 4, 3, 2, 1]