Python is an object-oriented programming language that provides rapid application development which was released in 1991 by Guido van Rossum. It has a huge demand in Rapid Application Development due to its dynamic binding and typing options. Some features of Python, which make it the most popular programming language right now is that it is free and open-source, has a vast standard library, and it can be integrated with other programming languages easily.
What Is Python?
Python is a general-purpose programming language that is often applied to scripting roles. Scripting languages tend to be limiting, but Python, on the other hand, is an uncomplicated and robust programming language that delivers both the power and complexity of traditional style. Python is designed to be very efficient in writing and especially to read.
Introduction To Python List
A Python list is a collection of zero or more elements. An element of the list can be any data. It can be string, numerical, or a combination of both types. A list in python is like an array in C or Java. Lists are mutable- that is, you can change their content-and they have many useful specialized methods.
How To Create A Python List?
A Python list is created by adding elements in the square brackets [ ].
num = [1, 2, 3, 4]
letter = [‘a’, ‘b’, ‘c’, ‘d’]
Fig: Create the Python List
These lists can contain elements of different types.
Access Elements From List
Every Python list starts with the index ‘0’. We can access the elements by specifying the index number. For example: To access elements in the list, we provide the index (as integer) inside the square brackets ( [ ] ) as given below.
Fig: Access elements from starting of the list
To access the elements from the end we use negative indexing. -1 means the last element. -2 the second last element and so on. For instance, if you want to access the 4th element from the ending of the list named mix, you write mix[-4].
Fig: Access the elements from ending of the list
You can also access the elements from a specific range. To get elements from index 1 to index 3 from the list mix, you should write mix[1:4].
Fig: Access the elements from a specific range
Remember that the first item is position 0.
Note: The search will start at index 1 (included) and end at index 4 (not included)
If the starting value is left out, the range will begin from the starting element. For instance:
Fig: Get elements from Index 0
Change And Add Elements To The List
In a Python list, the element value can be changed by specifying the index number and replacing it with a new value. For instance:
Fig: To change the element in Python List
For adding new elements to a list in Python, you can use the ‘append ()’ method or ‘insert ()’ method.
1. Using the Append () Method
The append() method adds the element to the end of the list. For example:
Fig: Adding element by using append () method
2. Using the Insert () Method
If you want to insert an element at a specific index, you should use the insert () method.
Fig: Add element using insert () method
Delete Elements From A List
There are two ways of removing elements from a list:
1. Delete specified elements we use remove () method.
Suppose you want to delete the item “get” from the list “mix”. We write it as mix.remove(“get”).
Fig: To delete element by using remove ()
2. To remove the element from specific index we use del () method
Syntax: del list_name [index]
Fig: Deleting element using the del () method
Check If The Item Exists In List
You can check if the particular element exists in the list or not. The following example can be used to perform the same:
Fig: Check if the item exists in the list
Check The Length of The List
You can check the number of items in the list by using ‘len ()’ method
Fig: To check the number of items in the list
To Join The Two List
There are several ways to join two lists, and the easiest method is to use the ‘ + ‘ operator. This can be done in the following way:
Fig: Join two Python lists
Built-in Functions In Python List
Python consists of some in-built functions that we can use.
Removes all the elements from the list
Returns a copy of the list
Adds an element to the end of the list
Add an element to the specified position
Removes the element from the specified position
Removes the item with the specified value
Reverses the order of the list
Returns the number of elements with the specified value
Returns the index of an element in the list
Here are some examples of the functions that are mentioned in the table:
1. Clear ()
The clear() function removes all the elements from the list.
Syntax: mix.clear ()
Fig: Clear a Python list
2. Copy ()
The copy() function returns a copy of all the items in a list.
Syntax: new_list = mix.copy()
Fig: Copy one Python list to another list
3. reverse ()
The reverse method prints the items in a list in the reverse order.
Syntax: list.reverse ()
Fig: Reverse a Python List
3. index ()
The index method returns the index of the specified item in a list.
Fig: Find the index in a list
Learn data operations in Python, strings, conditional statements, error handling, and the commonly used Python web framework Django with the Python Training course.
In this article, we have discussed the Python list and all the essential operations that we can perform. The list is an important data structure that plays a crucial role in learning the basics of python.