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 0You 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:
- Python Hello World Program
- Python Comment | Creating a Comment | multiline comment | example
- Python Dictionary Introduction | Why to use dictionary | with example
- How to do Sum or Addition in python
- Python Reverse number
- find the common number in python
- addition of number using for loop and providing user input data in python
- Python Count char in String
- Python Last Character from String
- Python Odd and Even | if the condition
- Python Greater or equal number
- Python PALINDROME NUMBER
- Python FIBONACCI SERIES
- Python Dictionary | Items method | Definition and usage
- Python Dictionary | Add data, POP, pop item, update method
- Python Dictionary | update() method
- Delete statement, Looping in the list In Python
- Odd and Even using Append in python
- Python | Simple Odd and Even number
Get Salesforceย Answers