# Summary of Week 7 Activities at the Blockfuse Labs: # Python List Python list is an ordered and mutable (changeable) collection that stores items inside square brackets [], and separated by commas. When we say Python list is mutable, it simply means that you can access, add, and remove items after the list has been created. Lists can include elements of any data type, including booleans, integers, strings, dictionaries, or even other lists (nested list). Accessing items in Python List can be done directly using their position (index), starting from index 0. Example: **List of integers** a = [1, 2, 3, 4, 5] **List of strings** b = ['Guava', 'apricot', 'strawberry'] **List of Mixed data types** c = ["Paul", 10, 20, "John", 40, True] print(a) print(b) print( c) ``` The output of the above are: [1, 2, 3, 4, 5] ['Guava', 'apricot', 'strawberry'] ["Paul", 10, 20, "John", 40, True] ``` **Accessing List Elements** Elements in a list can be accessed using indexing. Python indexes start at 0, so an element at index [0] will access the first element, while negative indexing allows us to access elements from the end of the list. Like index [-1] represents the last elements of list. Example: ``` fruits = ['Guava', 'apricot', 'strawberry'] print(fruits[0]) # 'Guava' print(fruits[-1]) # 'strawberry' ``` **Updating, Adding and Removing List Elements** We can change the value of an element by accessing it using its index. In Python, an index is the numbered position of an element inside a sequence like a list, or string. Indexing starts at 0. We can add elements to a list using the following methods: ``` append(): Adds an element at the end of the list. for example: fruits.append('apple') print(fruits) # ['Guava', 'apricot', 'strawberry', 'apple'] extend(): Adds multiple elements to the end of the list. fruits.extend(['mango', 'pawpaw']) print(fruits) # ['Guava', 'apricot', 'strawberry', 'apple' 'mango', 'pawpaw'] insert(): Adds an element at a specific position for example: fruits.insert(0, 'pear') print(fruits) # ['pear', Guava', 'apricot', 'strawberry', 'apple', 'mango', 'pawpaw'] ``` We can also delete elements in a list using the following: ``` remove(): Removes the first occurrence of an element. For example: fruits.remove('apple') print(fruits) #this removes apple from the list pop(): Removes the element at a specific index or the last element if no index is specified. popped_fruit = fruits.pop(1) print( popped_fruit) #shows the fruit that was removed at index 1 print(fruits) #prints the remaining fruits after removing the fruit at index one. del statement: Deletes an element at a specified index. ``` **Conclusion** Python lists are versatile and powerful data structures that allows the user to store and manipulate several objects in a single container. They provide advantages such as dynamic resizing, simple element access via indexing, and support for a variety of operations such as adding, removing, and updating elements. Lists are frequently used in Python because of their flexibility, simplicity, and efficiency in processing collections of data, making them an essential component in many algorithms and applications. (https://www.justacademy.co/). **References** 1. Lists in Python; https://www.justacademy.co/ 2. Python List; https://www.geeksforgeeks.org › python › python-lists 3. Python Lists; https://mimo.org/glossary/python/lists