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
- Ordered
lists are ordered, it means that the items have a defined order, and that order will not change. If we add new items to a list, the new items will be placed at the end of the list.
- Changeable
The list is changeable, meaning that we can change, add, and remove items in a list after it has been created.
- Allow Duplicates
Since lists are indexed, lists can have items with the same value.
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
- using for loop:
my_list = ["apple", "banana", "cherry"] for x in my_list: print(x)
- using Index Numbers . use the range() and len() function:
my_list = ["apple", "banana", "cherry"] for x in range(len(my_list)): print(my_list[x])
- using while loop:
We can use the len() function to determine the length of the list, then start at 0 and loop through the list items by refering to their indexes. Remember to increase the index by 1 after each iteration.
my_list = ["apple", "banana", "cherry"] i = 0 while i < len(my_list): print(my_list[i]) i = i + 1
- using List Comprehension:
my_list = ["apple", "banana", "cherry"] [print(x) for x in my_list]
Sorting a List
- Sort List Alphanumerically. List objects have a sort() method that will sort the list alphanumerically, ascending, by default:
my_list = ["orange", "mango", "kiwi", "pineapple", "banana"] my_list.sort() print(my_list)
- Sort List Descending. To sort descending, use the keyword argument reverse = True:
my_list.sort(reverse = True) print(my_list)
- Case Insensitive sort. By default the sort() method is case sensitive, resulting in all capital letters being sorted before lower case letters. we can use built-in functions as key functions when sorting a list. So if we want a case-insensitive sort function, use str.lower as a key function
my_list.sort(key = str.lower) print(my_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:
- using built-in copy() method.
my_list2 = my_list.copy() print(my_list2)
- using built-in list() method.
my_list2 = list(my_list) print(my_list2)
Join Lists
There are several ways to join, or concatenate, two or more lists in Python.
- using + operator.
my_list1 = ["apple", "banana", "cherry"] my_list2 = [1 , 2, 3, 4, 5] my_list3 = my_list1 + my_list2 print(my_list3)
- appending all the items from one list to another, one by one:
for x in my_list2: my_list1.append(x) print(my_list1)
- using extend() method to add one list at the end of another:
my_list1.extend(my_list2) print(my_list1)