Hey, welcome back to Stechies. In this article, we'll learn about Python List and following operations that can be performed on them:
- What is Python List?
- Creating List in Python
- Accessing List Items in Python
- Negative Indexing of Python List
- Range of Indexes/ Slicing in Python
- Changing List Item in Python
- Adding Items in Python List
- Removing Items from Python List
- Iterating Through a Python List
- Python List Membership Test
- Joining Two List in Python
- Finding Length of the List in Python
What is Python List?
So, what exactly is a python list? Well, the list is one of the 6 built-in types of sequences available in python. Now, what is a Sequence? The sequence is one of the most basic data structures available in python.
The list in python can be defined as a collection of ordered and changeable items.
As lists are ordered, items can be called individually by referring to their index number.
Also, the items in the list can be manipulated and hence the list is said to be mutable.
Creating List in Python
In the python, the list is written within square brackets with commas separating multiple values. A list can hold multiple data types i.e the list could be of string, integer or float type or all combined.
Example
#Empty List
MyList = [ ];
#List with Integer values
MyList_1 = [1, 2, 3, 4 ];
#List with mixed values
MyList_2 = [ 'Hello', 'this is my first list', 'of the year', 2020 ];
Accessing List Items
We can access the item of the list by referring to the index number associated with that particular item. Indexing starts at 0, so the first item of the list will have an index number 0, the second item will have an index number 1 and so on.
So let's create a list and access its item.
Example
#Example
MyList = ['India', 'USA', 'UK', 'Russia'];
#accessing item
print(MyList[1])
Output:
USA
Since 'USA' have indexing number 1
Negative Indexing of List
Python allows the concept of negative indexing.
In negative indexing, the indexing starts from the end of the list i.e the last item has an index number -1, second last -2 and so on.
Example
MyList = ['India', 'USA', 'UK', 'Russia'];
#Accessing using negative index
print(MyList[-2])
Output
UK
Since the UK has indexing number of -2
Range of Indexes/ Slicing
We can specify the range of list by providing 2 indexes number one is the beginning and the other end of the range. This will help to print the items of the list within the specified range.
Example
MyList = ['India', 'USA', 'UK', 'Russia', 'Japan', 'China'];
#Accessing
print(MyList[2:5])
Output
['UK', 'Russia', 'Japan']
NOTE: The item at index 5 will NOT be included.
To print the whole list from index 2 use print(MyList[2:])
Changing List Item
We can change the value of a specific item by referring to its index number.
#Example
MyList = ['India', 'USA', 'UK', 'Russia', 'Japan', 'China'];
#Changing 'USA' to 'Europe'
MyList[1] = 'Europe'
#Printing MyList
print(MyList)
Output
['India', 'Europe', 'UK', 'Russia', 'Japan', 'China']
Adding Items in List
We can add new items to the list using the append() method. This method will add a new item to the end of the list.
Example
MyList = ['India', 'USA', 'UK', 'Russia'];
#Using append() to add 'Australia'
MyList.append('Australia')
print(MyList)
Output
['India', 'USA', 'UK', 'Russia', 'Australia']
To add the item at a specific index we use insert() method.
Example
MyList = ['India', 'USA', 'UK', 'Russia'];
#Using insert() to add 'Australia' at index 1
MyList.insert(1, 'Australia')
print(MyList)
Output
['India', 'Australia', 'USA', 'UK', 'Russia'];
Removing Items from List
The following are the methods for removing items from the list.
1) Remove() Method
- remove() method removes the specified item.
Example
MyList = ['India', 'USA', 'UK', 'Russia'];
#Using remove() method
MyList.remove('Russia')
print(MyList)
Output
['India', 'USA', 'UK']
2) Pop() Method
- pop() method removes the item at the specified index, if the index is not specified then it removes the last item.
Example
MyList = ['India', 'USA', 'UK', 'Russia'];
#using pop() method
MyList.pop()
print(MyList)
Output
['India', 'USA', 'UK']
3) Del Keyword
- del keyword removes the item at the specified index
Example
MyList = ['India', 'USA', 'UK', 'Russia'];
#Using del keyword
del MyList[3]
print(MyList)
Output
['India', 'USA', 'UK']
Note: del keyword can also be used to delete the list completely
#Deleting the list
del MyList
print(MyList)
Output
Traceback (most recent call last):
File "main.py", line 2, in <module>
del MyList
NameError: name 'MyList' is not defined
#This will cause an error because we have successfully deleted 'MyList'
4. Clear() Method
- clear() method is used for emptying the list
Example
MyList = ['India', 'USA', 'UK', 'Russia'];
#Emptying the list
MyList.clear()
Output
[ ]
Iterating Through a List
We can iterate through each item in the list using the for loop.
Example
MyList = ['India', 'USA', 'UK', 'Russia'];
for x in MyList:
print(x)
Output
India
USA
UK
Russia
Python List Membership Test
We can test whether the item exists in the list or not by using the 'in' keyword. This returns the boolean value i.e true value if the item exists and a false value if it does not.
Example
MyList = ['India','USA', 'UK', 'Russia'];
print('India' in MyList)
Output
True
print('Japan' in MyList)
Output
False
Joining Two List in Python
1) Using + Operator
We can join/concatenate two or more than two lists using the “+” operator in python.
Example
MyList = ['India', 'USA', 'UK', 'Russia']
MyList_2 = ['Japan', 'China']
Final_List = MyList + MyList_2
print(Final_List)
Output
['India', 'USA', 'UK', 'Russia', 'Japan', 'China']
2) Using Append Function
We can also join two lists using the append() method as
Example
My_list = ['India', 'USA', 'UK', 'Russia'];
Final_List = ['Japan', 'China']
for x in Final_List:
My_list.append(x)
print(My_list)
Output
['India', 'USA', 'UK', 'Russia', 'Japan', 'China']
Finding Length of the List
Refer To This Page: Python: Find Length of List