Lists are ordered sequences that can hold a variety of object types. They use [] brackets and commas to separate objects in the list.

List items are ordered, changeable, and allow duplicate values.

List support indexing and slicing same as String and It can be nested as well

	
	my_list= ["String", 1 , 2.5, true]
	
	print(my_list[0])
	
	print(my_list[1:])
	

create an empty list

	
	my_list= []
	

List Properties


List Length

len() function is use to determine the length of a list.

	
	my_list= ["String", 1 , 2.5, true]
	
	print(len(my_list))
	

Check if Item Exists

To determine if a specified item is present in a list use the in keyword

	
	my_list = ["apple", "banana", "cherry"]
	
	if "apple" in my_list:
	
		print("Yes, 'apple' is in the fruits list")
	

Count the number of element with specified value

The count() method returns the number of elements with the specified value

	
	list.count(value)  // Syntax
	
	points = [1, 4, 2, 9, 7, 8, 9, 3, 1]

	x = points.count(9)
	

Change Item Value

To change the value of a specific item, refer to the index number.

	
	my_list = ["apple", "banana", "cherry", "orange", "kiwi", "mango"]
	
	my_list[1] = "blackcurrant"
	
		print(my_list)
		
	my_list[1:3] = ["blackcurrant", "watermelon"]
	
		print(my_list)
	

Insert Items

To insert a new list item, without replacing any of the existing values, we can use the insert() method. The insert() method inserts an item at the specified index:

	
	my_list = ["apple", "banana", "cherry"]
	
	my_list.insert(2, "watermelon")
	
		print(my_list)
	

Add Items to List

To add an item to the end of the list, use the append() method.

	
	my_list = ["apple", "banana", "cherry"]
	
	my_list.append("orange")
	
		print(my_list)
	

Remove Specified Item

The remove() method removes the specified item.

	
	my_list = ["apple", "banana", "cherry"]
	
	my_list.remove("banana")
	
		print(my_list)
	

Remove Specified Index

The pop() method removes the specified index. If we do not specify the index, the pop() method removes the last item.

	
	my_list = ["apple", "banana", "cherry"]
	
	my_list.pop(2)
	
		print(my_list)
	

The del keyword also removes the element from specified index. It also delete the list completely.

	
	my_list = ["apple", "banana", "cherry"]
	
	 del my_list[2]
	
		print(my_list)
		
	del my_list
	

Clear the List

The clear() method empties the list. The list still remains, but it has no content.

	
	my_list = ["apple", "banana", "cherry"]
	
	my_list.clear()
	
		print(my_list)
	

Loop Through a List

Sorting a List

Reverse Order

If we want to reverse the order of a list, regardless of the alphabet, we can use reverse() method, which reverses the current sorting order of the elements.

	
	my_list.reverse()
	
		print(my_list)
	

Copy Lists

We cannot copy a list simply by using assignment operator as list2 = list1, because: list2 will only be a reference to list1, and changes made in list1 will automatically also be made in list2. We can use following ways to copy a list:

Join Lists

There are several ways to join, or concatenate, two or more lists in Python.