Welcome to another post of our Python tutorial series, Learn Python with me. In this post, we will continue discussing the topics of Python lists. We will learn how to concatenate two lists, repeat lists, aliasing and cloning lists, and check the membership of elements in any list.
Link to all Python Chapters: Learn Python
How to concatenate two lists in Python?
The ‘+’ operator is widely used for concatenating any two entities in the world of programming. You can use the same operator to concatenate lists in Python, as mentioned in the program below.
#Python program to concatentate
#two lists
list1=[11,22,33,44,55]
print("The list1 is: \n",list1)
list2=[66,77]
print("The list2 is: \n",list2)
concatentated_list=list1+list2
print("The concatentated list is: \n",concatentated_list)
Output
The list1 is:
[11, 22, 33, 44, 55]
The list2 is:
[66, 77]
The concatentated list is:
[11, 22, 33, 44, 55, 66, 77]
How to repeat any list in Python?
We discussed in the earlier chapters related to strings that you can repeat a string to get displayed any number of times in the output using the ‘*’ operator. You can use the same operator to repeat the lists in Python programs.
#python program to repeat a list
list1=[10,20,30]
print("The declared list is: \n",list1)
print("\nLet us repeat the lists now\n")
n=int(input("How many times do you want this list to repeat? "))
print("The repeated list is: \n\n",list1*n)
Output
The declared list is:
[10, 20, 30]
Let us repeat the lists now
How many times do you want this list to repeat? 3
The repeated list is:
[10, 20, 30, 10, 20, 30, 10, 20, 30]
Aliasing and Cloning Lists in Python
If you give a new name to any existing list, it is called aliasing. If you perform aliasing on any list, the new alias list and the current list refer to the same list. Suppose you modify any one of the lists in the existing-aliasing duo pair. In that case, the other list you have not updated will get automatically updated as they refer to the same list. To understand this, let us now look at a program.
#python program to understand
#the concept of aliasing in lists
list1=[1,2,3,4,5]
list2=list1 #creating an alias
print("Before modification: ")
print("The list1: ",list1)
print("The alias list2: ",list2)
#modifying the first element of the list to 11
list1[0]=11
print("\n\nAfter modification")
print("The list1: ",list1)
print("The alias list2: ",list2)
Output
Before modification:
The list1: [1, 2, 3, 4, 5]
The alias list2: [1, 2, 3, 4, 5]
After modification
The list1: [11, 2, 3, 4, 5]
The alias list2: [11, 2, 3, 4, 5]
You can notice in the program above that we have created a list1 with five elements in it. We also created an alias list, list2. After this, when we updated the first element in the list1 to 11, the first element of list2 was also updated automatically to 11.
This is a significant disadvantage of creating aliases for existing lists in Python. However, if your requirement needs your lists to behave in this manner, you can create lists and their corresponding aliases in your Python program. But what if you do not want the new list or the existing list to get an automatic change when you update the other list? Do not bother thinking. In such cases, you can rely on the concept of cloning or copying in Python.
You can clone the existing list using the slicing operation or create a copy of the current list using the copy() method. Let us now look at a program to understand more about this concept.
#Python program to understand the concepts
#of cloning and copying
#CLONING CONCEPT
print("*********\nCLONING EXAMPLE\n*********\n")
a=[1,2,3,4,5] #create a new list a
b=a[:] #cloned the list a to list b
print("Before modification: ")
print("The list a: ",a)
print("The list b: ",b)
#let us modify the first element of list a to 100
a[0]=100
print("After modification: ")
print("The list a: ",a)
print("The list b: ",b)
#COPY Method concept
print("\n*********\ncopy() Method EXAMPLE\n*********\n")
p=[55,66,77,88,99]
q=p.copy() #copied list p to list q
print("Before modification: ")
print("The list p: ",p)
print("The list q: ",q)
#let us modify the third element of list q to 100
q[2]=100
print("After modification: ")
print("The list p: ",p)
print("The list q: ",q)
Output
*********
CLONING EXAMPLE
*********
Before modification:
The list a: [1, 2, 3, 4, 5]
The list b: [1, 2, 3, 4, 5]
After modification:
The list a: [100, 2, 3, 4, 5]
The list b: [1, 2, 3, 4, 5]
*********
copy() Method EXAMPLE
*********
Before modification:
The list p: [55, 66, 77, 88, 99]
The list q: [55, 66, 77, 88, 99]
After modification:
The list p: [55, 66, 77, 88, 99]
The list q: [55, 66, 100, 88, 99]
You can notice in the programs above that updating the elements after cloning or copying have not changed the elements in the corresponding list.
How to check the membership of elements in Python lists?
You can check whether an element is present in a list using the “in” and “not in” operators.
- If you use the “in” operator, it returns True if the element is present in the list. Otherwise, it returns False.
- If you the “not in” operator, it returns True if the element is not present in the list. Otherwise, it returns False.
#python program to check the
#concept of membership of elements
#in a list
a = [11,22,33,44,55]
#Let us check the "in" operator
print(11 in a)
print(99 in a)
print("\n")
#let us check the "not in" operator
print(11 not in a)
print(99 not in a)
Output:
True
False
False
True