Python List
List is an ordered collection of data or elements which are mutable and placed within the square brackets.
Example
fruits = ["mango", "grapes", "guava", "apple", "watermelon"]
print(fruits)`
Output
Access List Items
To access the items or elements of a list, you can simply refer to the item index number.
Example
fruits = ["mango", "grapes", "guava", "apple", "watermelon"]
print("Item at index 0 = "+ fruits[0])
print("Item at index 1 = "+ fruits[1])
print("Item at index 2 = "+ fruits[2])
print("Item at index 3 = "+ fruits[3])
print("Item at index 4 = "+ fruits[4])
Output
Item at index 1 = grapes
Item at index 2 = guava
Item at index 3 = apple
Item at index 4 = watermelon
Access List Items Using Negative Index
The negative index starts the search from the last item of the list in Python.
Example
names = ["Joe", "Danny", "Tom", "Ronie", "Victory", "Kelly", "Peter"]
print("Item at negative index -1 = " + names[-1])
print("Item at negative index -2 = " + names[-2])
print("Item at negative index -3 = " + names[-3])
Output
Item at negative index -2 = Kelly
Item at negative index -3 = Victory
Access List Items Using Range Index
Items of a list can also be accessed by specifying range of indexes, the start index and the end index separated by colon.
Example
colors = ["blue", "white", "red", "green", "orange", "purple", "brown"]
print(colors[0:3])
Output
Note: If you do not specify the end index, it will read till the last index.
Example
colors = ["blue", "white", "red", "green", "orange", "purple", "brown"]
print(colors[4:])
Output
Looping Through a List
You can loop through a list uring for loop.
Example
colors = ["blue", "white", "red", "green", "orange", "purple", "brown"]
for i in colors:
print(i)
Output
white
red
green
orange
purple
brown
List Length
In Python, the len() function is used to find the length of a list.
Example
colors = ["blue", "white", "red", "green", "orange", "purple", "brown"]
print(len(colors))
Output
Looping Through a List Using Range
You can also loop through a list using for loop with range() function.
Example
colors = ["blue", "white", "red", "green", "orange", "purple", "brown"]
for i in range(len(colors)):
print(colors[i])
Output
white
red
green
orange
purple
brown
Add Items to a List
To add items to a list, you can use the append() function that will add item at the end of the list items.
Example
nums = [12, 33, 45, 77, 23]
nums.append(100)
print(nums)
Output
Insert Items to a List
The insert() function is used to add items at the specified index of the list. In the following example, number 88 is inserted at index 2 of the list.
Syntax
insert(index, item)
Example
nums = [1, 2, 3, 4, 5]
nums.insert(2, 88)
print(nums)
Output
Add Multiple Items to a List
To add multiple items to a list using a single function, you can use the extend() function.
Syntax
extend([items separated by comma])
Example
nums = [10, 20, 30, 40, 50]
nums.extend([60, 70, 80, 90, 100])
print(nums)
Output
Remove Item from a List
To remove an item from a list, you can use the remove() function.
Syntax
remove(item)
Example
fruits = ["apple", "mango", "carrot", "grapes", "orange"]
fruits.remove("carrot")
print(fruits)
Output
Pop Item
You can also remove a list item by index number using the pop() function.
Example
fruits = ["apple", "mango", "carrot", "grapes", "orange"]
fruits.pop(2)
print(fruits)
Output
Note: If you do not specify the index number, the pop() function will remove the last item from the list.
Example
fruits = ["apple", "mango", "carrot", "grapes", "orange"]
fruits.pop()
print(fruits)
Output
Delete Multiple Items
You can delete multiple items from the list by using the del keyword.
Syntax
del list_object[del_from_index : del_to_index]
Example
fruits = ["apple", "mango", "carrot", "grapes", "orange"]
del fruits[2:]
print(fruits)
Output
Find Max Number From a List
To find the max number from a list in Python, you can use the max() function.
Example
nums = [20,77,33,45,10,40,56]
max_num = max(nums)
print(max_num)
Output
Find Min Number From a List
To find the min number from a list in Python, you can use the min() function.
Example
nums = [20,77,33,45,10,40,56]
min_num = min(nums)
print(min_num)
Output
Join Two Lists
In Python, there are so many ways to join two or more list together. Some of them can be achieved using the extend() function, the + operator, and the for loop.
Example 1
list1 = [2,3,4,5]
list2 = [6, 7, 8, 9, 10]
list1.extend(list2)
print(list1)
Output
Example 2
list1 = [2,3,4,5]
list2 = [6, 7, 8, 9, 10]
all_lists = list1 + list2
print(all_lists)
Output
Example 3
list1 = [2,3,4,5]
list2 = [6, 7, 8, 9, 10]
for i in list2:
list1.append(i)
print(list1)
Output
Copy List
In Python, you can copy a list to another list using the copy() function and the list() function.
Example 1
fruits = ["apple", "mango", "banana"]
new_fruits = fruits.copy()
print(new_fruits)
Output
Example 2
fruits = ["apple", "mango", "banana"]
new_fruits = list(fruits)
print(new_fruits)
Output
list() Constructor
In Python, you can use the list() constructor to create a new list.
Example
colors = list(("green", "blue", "green", "white"))
print(colors)
Output
Check If an Item Exists
You can use the in keyword to check if an item exists in a list.
Example
colors = ["green", "blue", "green", "white"]
if "blue" in colors:
print("blue Exists")