Python – List
As we read earlier about Python Loop. Now we will study about List in Python. In python programming language, we will cover define a list, create a list , list operations, list constructor python, how to add two lists in python etc. So let’s start reading about the first collection of data type.
What is a List in python ?
List is a collection / sequence of Heterogeneous data type. List is a mutable data type i.e. they can be edited or modified. In python list are written with Square Brackets.
List syntax python :
List variable = [ comma separated element ]
Python list Example : How to make a list ?
list_1 = [5, "Black", 3.5, "White", 75, "Blue"]
print(list_1)
Output :
[5, 'Black', 3.5, 'White', 75, 'Blue']
How to access the list from a list ?
We can access the list items by using the index number.
Example :
0 1 2 3 4 5
↓ ↓ ↓ ↓ ↓ ↓
list_1 = [5, "Black", 3.5, "White", 75, "Blue"]
print(list_1[3])
Output :
White
How to access the list items by using negative indexing ?
Negative indexing means beginning from the end i.e. – 1 refers to the last item, – 2 refers to the second last item, – 3 refers to the third last item.
List Example :
-6 -5 -4 -3 -2 -1
↓ ↓ ↓ ↓ ↓ ↓
list_1 = [5, "Black", 3.5, "White", 75, "Blue"]
print(list_1[-3])
Output :
White
How to access the list items by using Range of Indexes ?
Range of Indexes means where to start the range and where to end the range.
Example :
list_1 = [5, "Black", 3.5, "White", 75, "Blue"]
print(list_1[1:5])
Output :
['Black', 3.5, 'White', 75]
You can leave the start value blank, so the range will start at the first item
Example :
list_1 = [5, "Black", 3.5, "White", 75, "Blue"]
print(list_1[:4])
The above example returns the items from the beginning to “White”.
Output :
[5, 'Black', 3.5, 'White']
You can leave the end value blank, so the range will go to the end of the list
Example :
list_1 = [5, "Black", 3.5, "White", 75, "Blue"]
print(list_1[2:])
The above example return the items from ” 3.5 ” to end.
Output :
[3.5, 'White', 75, 'Blue']
How to access the list items by using Range of Negative Indexing ?
Negative indexing means beginning from the end i.e. – 1 refers to the last item , – 2 refers to the second last item, – 3 refers to the third last item.
Example :
list_1 = [5, "Black", 3.5, "White", 75, "Blue"]
print(list_1[-4:-1])
Output :
[3.5, 'White', 75]
How to change the item value ?
To change the value of a specific item, by using the index number.
Example :
list_1 = [5, "Black", 3.5, "White", 75, "Blue"]
list_1[3]="Red"
print(list_1)
Output :
[5, 'Black', 3.5, 'Red', 75, 'Blue']
Loop through a list
You can print all the list items by using the ” For” loop.
Example :
list_1 = [5, "Black", 3.5, "White", 75, "Blue"]
for i in list_1:
print(i)
Output :
5
Black
3.5
White
75
Blue
loops in detail : https://codifyshow.com/python-loop/
How to check the length of a given list ?
You can check the length of a given list, by using the len( ) function.
Example :
list_1 = [5, "Black", 3.5, "White", 75, "Blue"]
print(len(list_1))
Output :
6
Copy a list
You can make a copy of a list by using copy( ) function.
Example :
list_1 = [5, "Black", 3.5, "White", 75, "Blue"]
list_2 = list_1.copy()
print(list_2)
Output :
[5, 'Black', 3.5, 'White', 75, 'Blue']
There is an alternative method to make a copy of a list by using the built-in method list( ).
Example :
list_1 = [5, "Black", 3.5, "White", 75, "Blue"]
list_2 = list_1.copy()
print(list_2)
Output :
[5, 'Black', 3.5, 'White', 75, 'Blue']
Joining two list
You can join or concatenate two or more lists in python.
Example :
list_1 = ["Black", "White", "Red"]
list_2 = [5, 3.5, 75]
list_3 = list_1 + list_2
print(list_3)
Output :
['Black', 'White', 'Red', 5, 3.5, 75]
list( ) constructor
There is an alternative to make a new list by using the list( ) constructor.
Example :
list_1 = list(("Black", "White", "Blue"))
print(list_1)
Output :
['Black', 'White', 'Blue']
«Previous Next» >