Welcome to a new chapter of our Python tutorial series; Learn Python with me. In this post, we will begin discussing the concept of Python Lists by going through how to create a list using the range function, append the lists, update the lists and how to delete the elements from lists. Other topics related to the lists will also be covered in the subsequent chapters. Please use the link below to access the list of all chapters for your reference.
Link to all chapters related to Python: Learn Python
Python Lists?
A list is more like an array. Both array and list contain a set of elements. However, a significant difference makes lists more reliable than arrays. i.e., while an array can store a set of elements with the same type, a list can store elements with any type. For instance, you can have a list with int, float, and string data types of elements. Therefore, lists are more reliable, and you shouldn’t wonder if anyone tells you that the lists are the widely used data type in the Python world.
How to create lists in Python?
We can create lists by using square brackets. For example, please refer to the list ‘example’ mentioned below.
example = [1,2,3.5,4,7,"Mary","Christopher"]
print(example)
Output
[1, 2, 3.5, 4, 7, 'Mary', 'Christopher']
You can also create an empty list by not adding any elements, as shown below.
empty_list=[]
print(empty_list)
Output
[]
You can perform the operations such as Indexing and Slicing on any list. The indexing begins from 0, which means that the first element of the list would have the index as 0 and not 1. For instance, if you want to retrieve the string value “Mary” from the example list shared above, you must enter example[5], although this string is positioned at the sixth location. Since the indexing begins from 0, the example[5] would give us the output as “Mary”.
print(example[5])
Output
Mary
Let us now look at a program that creates different kinds of lists.
#a program to create different kinds of lists
#a list with float numbers
a=[1.5,2.5,3.5]
print("List with float elements")
print("Complete list: {}".format(a))
print("First element: {}, Last Element: {}".format(a[0],a[2]))
print("\n*****************\n")
#a list with string values
b=["Professor","Tokyo","Nairobi","Denver","Berlin","Rio"]
print("List with string elements")
print("Complete string list: {}".format(b))
print("First Name: {}, Last Name: {}".format(b[0],b[-1]))
print("\n*****************\n")
#a list with different kind of elements
c=[1,2,3.5,4,7,"Mary","Christopher"]
print("List with different kinds of elements")
print("The complete list: {}".format(c))
print("First element in the list: {}, Last Element in this list: {}".format(c[0],c[-1]))
print("End of the program")
Output
List with float elements
Complete list: [1.5, 2.5, 3.5]
First element: 1.5, Last Element: 3.5
*****************
List with string elements
Complete string list: ['Professor', 'Tokyo', 'Nairobi', 'Denver', 'Berlin', 'Rio']
First Name: Professor, Last Name: Rio
*****************
List with different kinds of elements
The complete list: [1, 2, 3.5, 4, 7, 'Mary', 'Christopher']
First element in the list: 1, Last Element in this list: Christopher
End of the program
How to create lists using the range() function?
We know that a sequence of numbers can be created easily using the range function. Though we have discussed this concept in the previous chapters, let me take the liberty to share the format of the range function once again for your reference.
range(start, stop, stepsize)
Let us now look at a program to create a sequence of numbers using the range function and then store it in a list.
#program to create lists using range function
#create a list with numbers from 0 to 7
list_a=range(8)
real_list_a=list(list_a) #convert into list
print("Complete elements from real_list_a: ")
print(real_list_a)
#create list with values from 7 to 14
list_b=range(7,15)
real_list_b=list(list_b) #convert into list
print("Complete elements from real_list_b: ")
print(real_list_b)
#create list with only even numbers from 10 to 20
#with step size of 2
list_c=range(10,21,2)
real_list_c=list(list_c) #convert into list
print("Complete elements from real_list_c: ")
print(real_list_c)
#you can also use for loop to retrive elements
#let's retrieve elements from real_list_c using for loop
print("\n********\nprinting using the for loop\n")
for element in real_list_c:
print(element)
Output
Complete elements from real_list_a:
[0, 1, 2, 3, 4, 5, 6, 7]
Complete elements from real_list_b:
[7, 8, 9, 10, 11, 12, 13, 14]
Complete elements from real_list_c:
[10, 12, 14, 16, 18, 20]
********
printing using the for loop
10
12
14
16
18
20
In the program above, we created the first list, real_list_a, with numbers from 0 to 7. We then made the second list, real_list_b, with numbers from 7 to 14, and the third list, real_list_c, with even numbers between 10 and 20.
How to update elements in a list?
In Python, the lists are mutable so that you can update the elements in any list. You can delete, update or append elements in any list depending on your needs. Let us look at a program to understand these concepts.
#Python program to understand
#the concepts of append, update, and delete
#in lists
#Let us create a simple lists
list1=[10,20,30,40,50]
print("The declared list is: ",list1)
print("\n********\n")
#Let us append 60 to the list1
#append adds the element to the end of the list1
list1.append(60)
print("The list after appending is: ",list1)
#let us update the last element of the list1 to 55
list1[-1]=55 #since indexing begins from -1 from end
print("The list after updating the last element: ",list1)
#let us remove the last element using delete
del list1[-1]
print("The list after deleting the last element: ",list1)
Output
The declared list is: [10, 20, 30, 40, 50]
********
The list after appending is: [10, 20, 30, 40, 50, 60]
The list after updating the last element: [10, 20, 30, 40, 50, 55]
The list after deleting the last element: [10, 20, 30, 40, 50]
In the program above, you can notice that when we appended 60 to the list, it was added as the last list element. Later, when we updated the same previous element 60 to 55, we did so by using the index position -1 as mentioned in the program above. We also deleted the number 55 eventually from the list using the same index position.