-
1. Introduction to Python
-
2. Python Basics
-
3. Working with Data Structures
-
4. Functions and Modules
-
5. Object-Oriented Programming (OOP)
-
6. File Handling
-
7. Error and Exception Handling
-
8. Python for Data Analysis
-
9. Advanced Topics in Python
-
10. Working with APIs
-
11. Python for Automation
-
12. Capstone Projects
- 13. Final Assessment and Quizzes
3.1.1 Creating, accessing, slicing, and modifying lists
Lists in Python are powerful, flexible, and commonly used data structures. They allow you to store multiple items in an ordered sequence, which you can modify, access, and slice in various ways. Below is an explanation of how to create, access, slice, and modify lists.
1. Creating Lists
In Python, lists are created by enclosing items in square brackets [], with elements separated by commas.
Basic Syntax for Creating Lists:
list_name = [item1, item2, item3, ...]
-
Empty List: A list with no items.
empty_list = []
-
List with Items: A list containing integers, strings, and even other lists.
fruits = ['apple', 'banana', 'cherry'] numbers = [1, 2, 3, 4, 5] mixed_list = [1, 'hello', 3.14, True]
2. Accessing List Elements
Once a list is created, you can access its elements using indexing. Python uses zero-based indexing, meaning the first element of a list is at index 0, the second is at index 1, and so on.
Accessing Elements Using Indexing:
fruits = ['apple', 'banana', 'cherry'] # Accessing the first element print(fruits[0]) # Output: apple # Accessing the second element print(fruits[1]) # Output: banana # Accessing the last element using negative index print(fruits[-1]) # Output: cherry
- Negative Indexing: You can use negative indices to access elements from the end of the list. The last element is -1, the second-last is -2, and so on.
Example:
fruits = ['apple', 'banana', 'cherry'] print(fruits[-2]) # Output: banana (second last element)
3. Slicing Lists
Slicing allows you to extract a sublist (a portion) from the original list. It is done by specifying a start index and an end index using the syntax list[start:end].
- Start Index: The index from where the slice starts (inclusive).
- End Index: The index where the slice ends (exclusive).
Syntax for Slicing:
sublist = list[start:end]
- Ommiting start: If the start index is omitted, slicing starts from the beginning of the list.
- Ommiting end: If the end index is omitted, slicing goes until the end of the list.
Example:
fruits = ['apple', 'banana', 'cherry', 'date', 'elderberry'] # Extracting a portion from index 1 to 3 (exclusive) print(fruits[1:4]) # Output: ['banana', 'cherry', 'date'] # Omitting the start index to get a sublist from the beginning print(fruits[:3]) # Output: ['apple', 'banana', 'cherry'] # Omitting the end index to get a sublist till the end print(fruits[2:]) # Output: ['cherry', 'date', 'elderberry'] # Reversing the list using slicing print(fruits[::-1]) # Output: ['elderberry', 'date', 'cherry', 'banana', 'apple']
4. Modifying Lists
Python lists are mutable, meaning you can modify, add, or remove elements from them.
Modifying Elements in a List:
To change the value of an item in a list, you can simply assign a new value to the specific index.
Example:
fruits = ['apple', 'banana', 'cherry'] # Modifying the second element fruits[1] = 'blueberry' print(fruits) # Output: ['apple', 'blueberry', 'cherry']
Adding Elements:
You can add elements to the list using various methods:
-
append(): Adds an item to the end of the list.
fruits.append('date') print(fruits) # Output: ['apple', 'blueberry', 'cherry', 'date']
-
insert(): Adds an item at a specific position.
fruits.insert(1, 'elderberry') # Adds 'elderberry' at index 1 print(fruits) # Output: ['apple', 'elderberry', 'blueberry', 'cherry', 'date']
-
extend(): Adds multiple items to the end of the list.
fruits.extend(['fig', 'grape']) print(fruits) # Output: ['apple', 'elderberry', 'blueberry', 'cherry', 'date', 'fig', 'grape']
Removing Elements:
You can remove elements using several methods:
-
remove(): Removes the first occurrence of a specified item.
fruits.remove('blueberry') print(fruits) # Output: ['apple', 'elderberry', 'cherry', 'date', 'fig', 'grape']
-
pop(): Removes and returns the item at a specified index. If no index is specified, it removes the last item.
popped_item = fruits.pop(2) # Removes and returns the element at index 2 print(fruits) # Output: ['apple', 'elderberry', 'date', 'fig', 'grape'] print(popped_item) # Output: 'cherry'
-
clear(): Removes all items from the list.
fruits.clear() print(fruits) # Output: []
Summary of Operations:
- Creating a List: Use square brackets to create a list, with items separated by commas.
- Accessing List Elements: Use positive or negative indexing to access elements.
- Slicing a List: Extract a sublist using start and end indices.
- Modifying a List: Lists are mutable, so you can change, add, or remove elements.
These operations form the foundation for working with lists in Python, and understanding them is essential for effective Python programming.
Commenting is not enabled on this course.