Hello, future programmer! Today, we're delving into Python Lists. These versatile tools can hold multiple items together. Picture a shopping list — it consists of various individual items consolidated under one name. Our mission is to comprehend Python Lists, including their creation, modification, and operations.
Python lists are ordered collections of items. 'Ordered' implies that each item has a predetermined position, akin to the arrangement of books on a shelf. Lists can store anything — numbers, strings, or even other lists! They are mutable (you can modify them) and allow duplicates.
Creating a list in Python is like packing a bag. Here, []
denotes the bag (or list), and the items are separated by commas. A list can contain various types of items — numbers, text, and even more lists! We can also create a list with the list()
function:
Python1fruits = ['apple', 'banana', 'cherry'] # creates a list of three elements 2numbers = list((1, 2, 3)) # creates a list of three elements - 1, 2, and 3 3print(fruits, numbers) # Outputs: ['apple', 'banana', 'cherry'] [1, 2, 3]
Accessing an item in a list is as effortless as calling its position in the order, starting with 0. On top of that, to access the elements from the end, you can use a specific notation with negative indices, starting from -1
. Here is an example:
Python1print(fruits[0]) # Prints the first element in the list, 'apple'. 2print(fruits[1]) # Prints the second element in the list, 'banana'. 3print(fruits[-1]) # Prints the last element in the list, 'cherry'. 4print(fruits[-2]) # Prints the second last element in the list, 'banana'.
To retrieve multiple items, we can use a slicing mechanism, specifying the start (inclusive) and the end (exclusive) indices to include:
Python1print(fruits[0:2]) # Prints ['apple', 'banana'] - 0-th, 1-st, but not the 2-nd element 2print(fruits[1:2]) # Prints ['banana'] - 1-st, but not the 2-nd element
Python provides various tools that allow us to add, remove, or alter items in a list or to check if a particular item exists:
Python1fruits.append('mango') # Append the item to the end of the list; the list becomes ['apple', 'banana', 'cherry', 'mango'] 2fruits.insert(1, 'pear') # Insert 'pear' at index 1; the list becomes ['apple', 'pear', 'banana', 'cherry', 'mango'] 3fruits.remove('banana') # Remove an item by value; the list becomes ['apple', 'pear', 'cherry', 'mango'] 4del fruits[0] # Remove the first element in the list; the list becomes ['pear', 'cherry', 'mango'] 5fruits[0] = 'mango' # Change the first item; the list becomes ['mango', 'cherry', 'mango'] 6fruits[2] = 'orange' # Change another item; the list becomes ['mango', 'cherry', 'orange'] 7print('kiwi' in fruits) # Check if 'kiwi' exists in the fruits list; Prints False
Python provides several built-in functions and methods for lists. Here's an example:
Python1numbers = [102, 51, 2035, 32, 989] 2print(len(numbers)) # Number of elements in the list; Prints 5 3print(min(numbers)) # Minimal item in the list; Prints 32 4print(max(numbers)) # Maximal item in the list; Prints 2035
Excellent work! You’ve mastered Python lists! You've learned how to create, modify, access list items, and perform operations, in addition to the utilization of built-in functions and methods.
Are you ready for practice? The upcoming exercises will solidify your understanding, helping you transition from a beginner to a confident Python programmer. Let's get started!