Python
In this tutorial we will learn about list in Python.
We briefly talked about list in the Python - Data Type tutorial.
A list is an ordered sequence of items. We use square brackets [ ] to create a list in Python.
[ ]
The items of a list are comma , separated and can be of multiple types like integer, floating point number, string, boolean, complex number.
,
A list can have an item repeated multiple times.
In the following example we are creating a list of integer numbers.
myList = [1, 2, 3]
list()
We use the list() constructor to create a list in Python.
# list myList = list((1, 3.14, 'yusufshakeel', 1+2j, True)) print("Type of myList:", type(myList)) print(myList)
Output
Type of myList: <class 'list'> [1, 3.14, 'yusufshakeel', (1+2j), True]
Items of a list are indexed and the first item of the list gets index 0, the second item of the list gets index 1 and so on.
Index of a list in Python is an integer value like other programming languages - C and Java.
If a list has 10 items then the last item will be at index (n - 1) i.e. 9.
We access list items using list[index] notation.
list[index]
Where, list represents the list variable and index is the index of the item that we want to access.
list
index
In the following example we are printing the items at index 1 and 3 for the given list.
# list myList = [1, "two", True, 3.14, 2+3j] print(myList[1]) # "two" print(myList[3]) # 3.14
We manipulate (change) list items using list[index] = value notation.
list[index] = value
Where, list denotes a list variable, index is the index of the item that we want to change and value is the new value that we are assigning to the list item at the given index.
value
In the following Python program we are changing the value at index 3.
# list myList = [1, "two", True, 3.14, 2+3j] print("Before", myList) # change myList[2] = False print("After", myList)
We will get the following output.
Before [1, 'two', True, 3.14, (2+3j)] After [1, 'two', False, 3.14, (2+3j)]
We can find the total number of items present in a given list using the len() method.
len()
In the following Python program we are printing out the total number of items present in the given list.
# list myList = [1, "two", True, 3.14, 2+3j] print("Total number of items:", len(myList))
Total number of items: 5
We can use for loop and while loop to loop through items of a given list.
In the following example we are printing all the items of a given list using for loop.
for
# list myList = [1, "two", True, 3.14, 2+3j] for i in myList: print(i)
The above Python program will give us the following output.
1 two True 3.14 (2+3j)
We can also use the while loop to achieve the same result.
while
# list myList = [1, "two", True, 3.14, 2+3j] i = 0 while i < len(myList): print(myList[i]) i += 1
If we want to check whether a given item is present or not present in a list we use the in - membership operator.
If the item we are looking for exists in the list then we will get True, otherwise False.
True
False
In the following Python program we are checking if 3 and "Hello" are present in the given list.
# list myList = ["Food", 2, 3.14, "Hello", False] # is 3 present if 3 in myList: print("3 is present in the list") else: print("3 is not present in the list") # is "Hello" present if "Hello" in myList: print("Hello is present in the list") else: print("Hello is not present in the list")
The program will give us the following output.
3 is not present in the list Hello is present in the list
In the next tutorial we are going to learn about useful list methods to work with lists.
See you there. Have fun coding :)