A Python list is an ordered collection of items, which can be of different types, including lists. Lists are created using square brackets [] and the items are separated by commas.
the following is a list of strings:
test_list = ['John', 'Lucy', 'Mike', 'Harry']
Lists can contain any data type, including strings, integers, floating point numbers, booleans, and even other lists.
test_list = ['John', 1234567890, 3.75, True, ['Ron', 'Hermoine']]
Basic Functions and Methods for Python List
One can modify the contents of a list by assigning new values to specific positions through indexing or by using list functions such as len(), type(), and methods such as append(), insert(), and extend(). You can also delete items from a list using the remove() or pop() methods.
len(): the len() function is a Python built-in function that returns the number of elements (length) of an object. When applied to a list, it returns the number of elements in that list.
test_list = ['John', 'Lucy', 'Mike', 'Harry']
print(len(test_list))
Output: 4
type(): the type() function in a Python built-in function that returns the type of an object. When applied to a list, it returns the type 'list'.
test_list = ['John', 'Lucy', 'Mike', 'Harry']
print(type(test_list))
Output: <class 'list'>
append(): the append() method of a list is used to add an item (element) to the end of a list.
test_list = ['John', 'Lucy', 'Mike']
test_list.append('Harry')
print(test_list)
Output: ['John', 'Lucy', 'Mike', 'Harry']
The append() method adds the item 'Harry' to the end of the list 'test_list'. It does not return a new list, but rather modifies an existing list in place.
You can use append() to add any type of item to a list, including numbers, strings, and even other lists.
test_list = ['John', 'Lucy', 'Mike']
test_list.append(1234567890)
print(test_list)
Output: ['John', 'Lucy', 'Mike', 1234567890]
test_list.append([1234567890, 2345678901, 3456789012])
print(test_list)
Output: ['John', 'Lucy', 'Mike', [1234567890, 2345678901, 3456789012]]
Keep in mind that the append() method only adds a single item to the end of the list. If you want to add multiple items, you can use the extend() method or the += operator which is basically an action of concatenation.
extend(): the extend() method is used to extend a list by appending all the items from a given list to the end of the list.
test_list = ['John', 'Lucy', 'Mike']
test_list.extend([1234567890, 2345678901, 3456789012])
print(test_list)
Output: ['John', 'Lucy', 'Mike', 1234567890, 2345678901, 3456789012]
test_list += [1234567890, 2345678901, 3456789012]
print(test_list)
Output: ['John', 'Lucy', 'Mike', 1234567890, 2345678901, 3456789012]
The extend() method does not return a new list, but rather modifies an existing list in place. It is similar to using the += operator to concatenate two lists, but it can be used to extend the list with any iterable, not just another list.
Keep in mind that the extend() method only adds items to the end of an existing list. If you want to add a single item, you can use the append() method instead.
insert(): the insert() method is used to insert an item (element) at a given position in an existing list. One additional argument i.e. index of the position where the item will be inserted would be required to be provided.
test_list = ['John', 'Lucy', 'Harry']
test_list.insert(2, 'Mike')
print(test_list)
Output: ['John', 'Lucy', 'Mike', 'Harry']
The insert() method does not return a new list, but rather modifies an existing list in place. It can be used to insert items at any position in the list, including at the beginning, the end, and in the middle.
Keep in mind that the index of the first item in the list is 0, and the index of the last item is len(my_list) - 1. If you try to insert an item at an index that is outside the range of the list, you will get an IndexError exception.
You can also use the insert() method to insert multiple items at once by providing a list as the second argument.
test_list = ['John', 'Lucy', 'Mike']
test_list.insert(2, [1234567890, 2345678901, 3456789012])
print(test_list)
Output: ['John', 'Lucy', [1234567890, 2345678901, 3456789012], 'Mike']
test_list.insert(3, [1234567890, 2345678901, 3456789012])
print(test_list)
Output: ['John', 'Lucy', 'Mike', [1234567890, 2345678901, 3456789012]]
This will insert the items in the iterable as a single item at the specified position in an existing list.
remove(x): Removes the first occurrence of an item from the list.
remove(x) is a method that removes the first occurrence of an item from a list in Python. The x argument is the item to be removed.
Here is an example of how to use the remove() method:
my_list = [1, 2, 3, 4, 5, 6]
my_list.remove(4) # my_list is now [1, 2, 3, 5, 6]
my_list.remove(1) # my_list is now [2, 3, 5, 6]
my_list.remove(6) # my_list is now [2, 3, 5]
The remove() method does not return a new list, but rather modifies the original list in place. It removes the first occurrence of the item x from the list, starting from the beginning of the list and moving left to right. If the item is not found in the list, it raises a ValueError exception.
You can use the remove() method to remove items of any data type, including numbers, strings, and even lists. For example:
my_list = [1, 2, 3, [4, 5], 6]
my_list.remove([4, 5]) # my_list is now [1, 2, 3, 6]
my_list = [1, 2, 3, "hello", 6]
my_list.remove("hello") # my_list is now [1, 2, 3, 6]
If you want to remove all occurrences of an item from the list, you can use a loop or a list comprehension to create a new list that does not contain the item. For example:
my_list = [1, 2, 3, 4, 5, 6, 4]
new_list = [i for i in my_list if i != 4] # new_list is now [1, 2, 3, 5, 6]
pop(i): Removes and returns the item at a given position. If no index is specified, removes and returns the last item.
pop(i) is a method that removes and returns the item at a given position in a list in Python. The i argument is the index of the item to be removed. If no index is specified, the method removes and returns the last item in the list.
Here is an example of how to use the pop() method:
my_list = [1, 2, 3, 4, 5]
item = my_list.pop(2) # item is 3, and my_list is now [1, 2, 4, 5]
item = my_list.pop() # item is 5, and my_list is now [1, 2, 4]
item = my_list.pop(0) # item is 1, and my_list is now [2, 4]
The pop() method does not return a new list, but rather modifies the original list in place. It removes the item at the specified position and returns it. If the index is out of range, it raises an IndexError exception.
clear(): Removes all items from the list.
clear() is a method that removes all items from a list in Python. It is a fast and efficient way to empty a list.
Here is an example of how to use the clear() method:
my_list = [1, 2, 3, 4, 5]
my_list.clear() # my_list is now an empty list []
The clear() method does not return a new list, but rather modifies the original list in place. It removes all items from the list, leaving it empty.
You can use the clear() method to remove all items from a list of any data type, including numbers, strings, and even lists. For example:
my_list = [1, 2, [3, 4], 5]
my_list.clear() # my_list is now an empty list []
my_list = ["hello", "world"]
my_list.clear() # my_list is now an empty list []
If you want to create a new empty list, you can simply use the [] notation or the list() function. For example:
my_list = [] # creates an empty list
index(x): Returns the index of the first occurrence of an item in the list.
index(x) is a method that returns the index of the first occurrence of an item in a list in Python. The x argument is the item to be searched for.
Here is an example of how to use the index() method:
my_list = [1, 2, 3, 4, 5]
index = my_list.index(3) # index is 2
index = my_list.index(1) # index is 0
index = my_list.index(5) # index is 4
The index() method returns the index of the first occurrence of the item x in the list, starting from the beginning of the list and moving left to right. If the item is not found in the list, it raises a ValueError exception.
You can use the index() method to search for items of any data type, including numbers, strings, and even lists. For example:
my_list = [1, 2, [3, 4], 5]
index = my_list.index([3, 4]) # index is 2
my_list = ["hello", "world"]
index = my_list.index("world") # index is 1
count(x): Returns the number of occurrences of an item in the list.
count(x) is a method that returns the number of occurrences of an item in a list in Python. The x argument is the item to be counted.
Here is an example of how to use the count() method:
my_list = [1, 2, 3, 4, 5, 4, 3, 2, 1]
count = my_list.count(3) # count is 2
count = my_list.count(4) # count is 2
count = my_list.count(1) # count is 2
count = my_list.count(6) # count is 0
The count() method returns the number of occurrences of the item x in the list. It searches the entire list and counts all occurrences of the item, regardless of their position. If the item is not found in the list, the method returns 0.
You can use the count() method to count items of any data type, including numbers, strings, and even lists. For example:
my_list = [1, 2, [3, 4], 5, [3, 4]]
count = my_list.count([3, 4]) # count is 2
sort(): Sorts the items of the list in ascending order.
sort() is a method that sorts the items of a list in ascending order in Python. It is a fast and efficient way to sort a list of items.
Here is an example of how to use the sort() method:
my_list = [5, 2, 4, 1, 3]
my_list.sort() # my_list is now [1, 2, 3, 4, 5]
my_list = ["apple", "banana", "cherry"]
my_list.sort() # my_list is now ["apple", "banana", "cherry"]
The sort() method does not return a new list, but rather modifies the original list in place. It sorts the items of the list in ascending order, according to their natural order. For example, numbers are sorted in increasing numerical order, and strings are sorted in alphabetical order.
You can use the sort() method to sort lists of any data type that has a natural order, including numbers, strings, and even tuples. For example:
my_list = [(2, 3), (1, 2), (3, 1)]
my_list.sort() # my_list is now [(1, 2), (2, 3), (3, 1)]
my_list = [{"name": "Alice", "age
reverse(): Reverses the order of the items in the list.
reverse() is a method that reverses the order of the items in a list in Python. It is a fast and efficient way to reverse the order of a list of items.
Here is an example of how to use the reverse() method:
my_list = [1, 2, 3, 4, 5]
my_list.reverse() # my_list is now [5, 4, 3, 2, 1]
my_list = ["apple", "banana", "cherry"]
my_list.reverse() # my_list is now ["cherry", "banana", "apple"]
The reverse() method does not return a new list, but rather modifies the original list in place. It reverses the order of the items in the list, so that the first item becomes the last, the second item becomes the second-to-last, and so on.
You can use the reverse() method to reverse the order of items in a list of any data type, including numbers, strings, and even tuples. For example:
my_list = [(2, 3), (1, 2), (3, 1)]
my_list.reverse() # my_list is now [(3, 1), (1, 2), (2, 3)]
my_list = [{"name": "Alice", "age": 30}, {"name": "Bob", "age": 25}, {"name": "Charlie", "age": 35}]
my_list.reverse() # my_list is now [{"name": "Charlie", "age": 35}, {"name": "Bob", "age": 25}, {"name": "Alice", "age": 30}]
Lists are very versatile and can be used in many different applications, such as storing data, processing data, and performing tasks. They are an important tool in the Python programmer's toolkit, and it is essential to understand how to use them effectively.
Comments