In this tutorial, we are going to tell all about Python list. The following is the content of this article.
- Introduction
- Negative indexing
- Access items
- Range of index
- Change item
- Loop in list
- List length
- Add Item
- Remove Item
- Copy a list
- Join list
- Methods
Introduction
In Python, the list is a collection of items of different data types just like tuple and set. It is an ordered sequence of items. A list object can be of one or more items, can be of the same type or different types, which are separated by a comma and enclosed in square brackets [].
Negative Indexing
The indexing will begin from the end character and so on.
fruits = ["grapes" , "apples" , "mango"]
print(fruits[-1]) #returns mango
Access Items
fruits = ["grapes" , "apples" , "mango"]
print(fruits[1]) #returns apples
Range of Indexes
We can select a range of items present in the list by using the range in indexing. The first index and the last index should be separated by a colon(:)
fruits = ["grapes" , "apples" , "mango" , "banana" , "lichi" ,"cucumber" , "guava"]
print(fruits[2:5]) #returns ['mango' , 'banana' , 'lichi' ]
Change Item
To change item value we can just put the item in that particular index as per our requirement.
fruits = ["grapes" , "apples" , "mango"]
fruits[1]= "banana"
print(fruits) #returns ['grapes' , 'banana' , 'mango']
Loop Using List
We can use a loop in a list
fruits = ["grapes" , "apples" , "mango"]
for x in fruits:
print(x)
List Length
To find the length of the list
fruits = ["grapes" , "apples" , "mango"]
print(len(fruits)) #returns 3
Add Items
In Python to add items into a list we use append() method.
fruits = ["grapes" , "apples" , "mango"]
fruits.append("banana")
print(fruits)
Remove Item
For removing an item from the list there are 3 methods in Python. One is remove() and second is pop() and last is del().
If we want to delete a particular index item we use remove() or pop() both. But if in case of pop() indexing is not declared it will pop out the last element. To know more you can refer to this site.
del() can delete the entire list same also for the clear() method.
fruits = ["grapes" , "apples" , "mango"]
fruits.remove("apples")
print(fruits)
fruits = ["grapes" , "apples" , "mango"]
fruits.pop()
print(fruits)
fruits = ["grapes" , "apples" , "mango"]
del fruits
print(fruits)
fruits = ["grapes" , "apples" , "mango"]
fruits.clear()
print(fruits)
Copy a List
There are ways to make a copy, one way is to use the built-in List method copy().
fruits = ["grapes" , "apples" , "mango"]
thelist = fruits.copy()
print(thelist)
Another way to make a copy is to use the built-in method list().
fruits = ["grapes" , "apples" , "mango"]
thelist = list(fruits)
print(thelist)
Join list
So to join two lists we can either simply use a “+” character or use extends() method.
fruits = ["mango", "guava" , "banana"]
flowers = ["rose", "jasmine", "sunflower"]
fruits.extend(flowers)
print(fruits)
List of methods in list
- append()
- clear()
- copy()
- count()
- extend()
- pop()
- remove()
- reverse()
- sort()
- index()
- copy()
Happy coding….