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ย sort,ย itโ€™sย helpfulย to knowย the properties of thatย sort.ย selectingย 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.