Python List Operations with Examples

What are lists in Python?

The list may be a collection that is ordered and changeable. permits duplicate members. A tuple may be a collection that is ordered and stable permits duplicate members. Set should be a collection that is unordered and unindexed. No duplicate members. Dictionary may be a collection that is unordered, changeable, and indexed. No duplicate members. When selecting a group sortit’s helpful to know the properties of that sortselecting the proper sort for a specific information set might mean retention of that means, and, it might mean a rise in efficiency or security.

Python List:

A list may be an assortment that is ordered and changeable. In Python list area unit written with square brackets {}.

Create a List in Python:

thislist = ["coconut", "banana", "mango"]
print(thislist)

Output:

C:\Users\Pramod\PycharmProjects\pythonProject\venv\Scripts\python.exe C:/Users/Pramod/PycharmProjects/pythonProject/main.py
['coconut', 'banana', 'mango']

Process finished with exit code 0

How to excess Element from List:

You access the list of things by bearing on the index number:

Example

Print the second item of the list:
thislist = ["apple", "banana", "cherry"]
print(thislist[1])

Output:

C:\Users\Pramod\PycharmProjects\pythonProject\venv\Scripts\python.exe C:/Users/Pramod/PycharmProjects/pythonProject/main.py
banana

Process finished with exit code 0

Change List of Item:

To change the value of a selected item, talk to the index number of the list:

Example:

Let’s we will change the second item of the list:
thislist = ["apple", "banana", "cherry"]
thislist[1] = "blackcurrant"
print(thislist)

Output:

C:\Users\Pramod\PycharmProjects\pythonProject\venv\Scripts\python.exe C:/Users/Pramod/PycharmProjects/pythonProject/main.py
['apple', 'blackcurrant', 'cherry']

Process finished with exit code 0

Loop Through a list:

You can loop through the list of things by employing a for loop:

Example:

Print all things within the list, one by one:
thislist = ["apple", "banana", "cherry"]
for x in thislist:
    print(x)

Output:

C:\Users\Pramod\PycharmProjects\pythonProject\venv\Scripts\python.exe C:/Users/Pramod/PycharmProjects/pythonProject/main.py
apple
banana
cherry

Process finished with exit code 0
You will learn a lot of concerning for loops in our Python For Loops Chapter.

Check if Item Exists in List:

To determine if a small item is present in a list using the keyword:

Example

Check if “apple” is present within the list:
thislist = ["apple", "banana", "cherry"]
if "apple" in thislist:
    print("Yes, 'apple' is within the fruits list")

Output:

C:\Users\Pramod\PycharmProjects\pythonProject\venv\Scripts\python.exe C:/Users/Pramod/PycharmProjects/pythonProject/main.py
Yes, 'apple' is within the fruits list

Process finished with exit code 0

Find the Length of List:

To determine the total number of list present in the list, use the len() method:

Example:

Print the number of things within the list:
thislist = ["coconut", "banana", "papaya","mango"]
print(len(thislist))

Output:

C:\Users\Pramod\PycharmProjects\pythonProject\venv\Scripts\python.exe C:/Users/Pramod/PycharmProjects/pythonProject/main.py
4

Process finished with exit code 0

Add Items into the list:

To add an element to the list, use the append() method:

Example:

Using the append() method to append particulat element item:

thislist = ["coconut", "banana", "mango"]
thislist.append("orange")
print(thislist)

Output:

C:\Users\Pramod\PycharmProjects\pythonProject\venv\Scripts\python.exe C:/Users/Pramod/PycharmProjects/pythonProject/main.py
['coconut', 'banana', 'mango', 'orange']

Process finished with exit code 0

Add item at require index Example:

To add some releven item at required index, use the insert() method:
thislist = ["coconut", "banana", "mango"]
thislist.insert(1, "orange")
print(thislist)

Output:

C:\Users\Pramod\PycharmProjects\pythonProject\venv\Scripts\python.exe C:/Users/Pramod/PycharmProjects/pythonProject/main.py
['coconut', 'orange', 'banana', 'mango']

Process finished with exit code 0

How to Remove List Item:

There will may be number of litem those of them some item you may be want to remove some item from the list:

Remove item using remove() method Example

To remove item from the list use remove() method to removes the required item from the list. When we use remove() method then we have to pass element as argument.
thislist = ["coconut", "banana", "mango","papaya"]
thislist.remove("banana")
print(thislist)

Output:

C:\Users\Pramod\PycharmProjects\pythonProject\venv\Scripts\python.exe C:/Users/Pramod/PycharmProjects/pythonProject/main.py
['coconut', 'mango', 'papaya']

Process finished with exit code 0

Remove item using pop() method Example

To remove item from the list use pop() method. The pop() method remove last index element of list by default but if you want to remove the particular index element then we have to pass index number.
thislist = ["coconut", "banana", "mango","papaya"]
thislist.pop(1) #removed item from inde no. 1
print(thislist)

Output:

C:\Users\Pramod\PycharmProjects\pythonProject\venv\Scripts\python.exe C:/Users/Pramod/PycharmProjects/pythonProject/main.py
['coconut', 'mango', 'papaya']

Process finished with exit code 0

Remove Item using del Keyword Example

To remove item from list you can also use del keyword. If you want to remove any particular indexed element use del keyword.
thislist = ["coconut", "banana", "mango","papaya"]
del thislist[1]
print(thislist)

Output:

C:\Users\Pramod\PycharmProjects\pythonProject\venv\Scripts\python.exe C:/Users/Pramod/PycharmProjects/pythonProject/main.py
['coconut', 'mango', 'papaya']

Process finished with exit code 0

Delete Complete List | Example

The del keyword can even delete the list completely:
thislist = ["coconut", "banana", "mango","papaya"]
del thislist

Clear() Method Example

To remove all item form list we can use clear() method:

thislist = ["coconut", "banana", "mango","papaya"]
thislist.clear()
print(thislist)

Output:

C:\Users\Pramod\PycharmProjects\pythonProject\venv\Scripts\python.exe C:/Users/Pramod/PycharmProjects/pythonProject/main.py
[]

Process finished with exit code 0

How to copy elements from one list to another in Python?

To copy item from one list to another list we will use copy() method. Copy() method dublicate the item or we can say that assigining one item list to another list.

Copy() Method Example:

Make a duplicate of an elements with the copy() method:
list1 = ["coconut", "banana", "mango","papaya"]
list2 = list1.copy()
print("Print list2 Items: ",list2)

Output:

C:\Users\Pramod\PycharmProjects\pythonProject\venv\Scripts\python.exe C:/Users/Pramod/PycharmProjects/pythonProject/main.py
Print list2 Items:  ['coconut', 'banana', 'mango', 'papaya']

Process finished with exit code 0

Another way to form a duplicate is to use the list() method.

Example2

Make a duplicate of an inventory with the list() method:
list1 = ["coconut", "banana", "mango","papaya"]
list2 = list(list1)
print("Print list2 Items: ",list2)

Output:

C:\Users\Pramod\PycharmProjects\pythonProject\venv\Scripts\python.exe C:/Users/Pramod/PycharmProjects/pythonProject/main.py
Print list2 Items:  ['coconut', 'banana', 'mango', 'papaya']

Process finished with exit code

Recommended Post:

Get Salesforce Answers

Pramod Kumar Yadav is from Janakpur Dham, Nepal. He was born on December 23, 1994, and has one elder brother and two elder sisters. He completed his education at various schools and colleges in Nepal and completed a degree in Computer Science Engineering from MITS in Andhra Pradesh, India. Pramod has worked as the owner of RC Educational Foundation Pvt Ltd, a teacher, and an Educational Consultant, and is currently working as an Engineer and Digital Marketer.