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.