In Python, a list is a data structure that acts like a container for holding a collection of items. These items can be numbers, strings, or even more complex objects. Lists are created using square brackets and the items inside are separated by commas. For instance, a list of numbers [1, 2, 3] or a list of things ['Apple', 'Banana', 'Orange']. Lists are helpful because they allow us to organize multiple pieces of data together. You can add new items to a list, remove items, change their order, or access specific items using their positions. Think of lists like a shopping list – you can add, remove, and rearrange items as needed. This flexibility and ease of use make lists an essential tool in Python for handling and managing various kinds of information.
I also learned many functions related to lists such as :
len(list): Returns the number of elements in the list.
list.append: Adds an item to the end of the list.
list. insert: Inserts an item at a specific index in the list.
list.extend : Adds elements from an iterable (another list, tuple, etc.) to the end of the list.
list.remove : Removes the first occurrence of a specified item from the list.
list.pop(index): Removes and returns the item at the specified index. If no index is provided, it removes and returns the last item.
These are some respective examples :
num_elements = len(my_list)
my_list.append(42)
my_list.insert(1, "item")
my_list.extend([1, 2, 3])
my_list.remove("item")
item = my_list.pop(2)