In this post, you will learn what is Python lists, how to create a list, how to adding or removing elements from python lists and so on.
Simultaneously tell you. Which type of in-build python methods use with python list. In-build methods are also included with the example. Which will make you easier to understand.
what is list in python?
The lists in python are Collections of items. which is ordered and changeable. The lists are defined by having values between square brackets [ ].
How to create a list?
To create a list in Python, place all the items within [] brackets and separate the items with commas.
A Python list can contain elements of different data types. Such as integer, float, string, etc.
python create empty list
First of all, we will create an empty list in python:
# empty list my_list = []
python create list with numbers
Now we will create a list in Python, in which list will be Numbers:
# list of integers my_list = [1, 2, 3]
python create list of string numbers
Now we will create another list which will contain numbers, strings, etc:
# list with mixed datatypes my_list = [1, "Hello", 3.4]
How to access elements from a list?
We learned how to create lists in Python with different data types.
Now we will learn how to access items / elements from lists.
python access list element by index
# list of integers my_list = [10, 20, 30] print(my_list[1])
As there is a list, from this list we have access to the items according to the index.
Output
20
python access list element by negative index
Negative indexing means beginning from the end, -1
refers to the last item, -2
refers to the second last item etc.
# list of integers my_list = [10, 20, 30] print(my_list[-1])
Output
30
python list get element by index range
You can get items in the range from the list. For this, the start and end range values have to be passed:
# list of integers my_list = [10, 20, 30, 40, 50, 60, 70, 80, 90] print(my_list[2:6])
Output
[30, 40, 50, 60]
Python find the length of list
If you want to find/get the list’s length, then you have to use the Python len() function.
You can see the below example how the list is find by length:
# list of integers my_list = [10, 20, 30, 40, 50, 60, 70, 80, 90] print(len(my_list))
Output
9
Add item to list python
Here you will learn how to add items to the list in Python.
The append () function is used to add items to the Python list. And the item is added to the end of the list.
# list of integers my_list = [10, 20, 30, 40, 50, 60, 70, 80, 90] my_list.append(100) print(my_list)
Output
[10, 20, 30, 40, 50, 60, 70, 80, 90, 100]
Add item to list at specific index python
If you want to add an item to the specific index of the list in Python, then you have to use insert() for it.
# list of integers my_list = [10, 20, 30, 40, 50, 60, 70, 80, 90] my_list.insert(1, 5) print(my_list)
Output
[10, 5, 20, 30, 40, 50, 60, 70, 80, 90]
Remove item from list python
By now you have learned how to add and access items in the list. Now you will learn how to delete/remove items from the Python list.
The remove() method is used to remove the first occurrence item with a specified value on python lists.
To delete/remove items from the Python list, use the function remove() of Python.
You can see the example below. How to remove/delete items from the list:
# list of integers my_list = [10, 20, 30, 40, 50, 60, 70, 80, 90] my_list.remove(90) print(my_list)
[10, 20, 30, 40, 50, 60, 70, 80]
python remove specific item from list
If you want to remove a specific item from the list of Python list. So for this you have to use pop () function.
You can see the example below. Let’s use the Python pop() function to remove the item from the specific index:
# list of integers my_list = [10, 20, 30, 40, 50, 60, 70, 80, 90] my_list.pop(2) print(my_list)
Note:- If you do not pass the index inside the pop() function, it will remove the value of last indexes in the python list.
Output
[10, 20, 40, 50, 60, 70, 80, 90]
If you want to remove items from the specific index of Python list, you can also use del keyword for this
Let’s take an example to understand better how to use the del keyword and remove the item from the list:
# list of integers my_list = [10, 20, 30, 40, 50, 60, 70, 80, 90] del my_list[3] print(my_list)
In the example given, we have removed the item that was on the 3rd index in python lists. You can see the output below:
Output
[10, 20, 30, 50, 60, 70, 80, 90]
python copy list of lists
In Python, you want to copy one list to another list. So for this, you have to use the copy () function of Python.
The copy () function copies one list into another list.
# list of integers my_list = [10, 20, 30, 40, 50, 60, 70, 80, 90] blist= my_list.copy() print(blist)
Output
[10, 20, 30, 40, 50, 60, 70, 80, 90]
python join two lists
If you want to join two lists in Python. So you can use its + operator. This will join your two lists.
Now we take an example. In this example, two lists are taken, one is a list of string data types and the other is a list of integer data types. We will use the + operator to connect to it:
list1 = ["a", "b" , "c"] list2 = [1, 2, 3] list3 = list1 + list2 print(list3)
Output
['a', 'b', 'c', 1, 2, 3]
Lists Methods Python
Below are more in-built methods for working with lists in Python:
Method | Description |
---|---|
append() | Adds an element at the end of the list |
clear() | Removes all the elements from the list |
copy() | Returns a copy of the list |
count() | Returns the number of elements with the specified value |
extend() | Add the elements of a list (or any iterable), to the end of the current list |
index() | Returns the index of the first element with the specified value |
insert() | Adds an element at the specified position |
pop() | Removes the element at the specified position |
remove() | Removes the item with the specified value |
reverse() | Reverses the order of the list |
sort() | Sorts the list |