Python Quick Guide - Step 1 Basic Syntax and Data Types (5) - Lists (1)
Series - Python Quick Guide
Contents
Info
- This course is designed to help you learn the basics of Python programming as quickly as possible through hands-on practice.
- The “Style Guide” sections primarily cover guidelines from PEP8 for writing clean Python code.
- You can run and see the results of each code example.
Feel free to experiment with the code - reloading the page will reset the content.
Step 1: Basic Syntax and Data Types
1.5. Organizing Data
1.5.1. Lists
- When you want to handle multiple elements together, you can use a list (
list
). - Lists are created by placing elements separated by commas (
,
) within square brackets ([
,]
).
Style Guide
- Always put a space after a comma (
,
).
- For long lists, spreading elements across multiple lines improves readability.
- Indent each element with 4 spaces.
- Include a trailing comma after the last element. (This prevents unnecessary line differences when adding elements later)
Basic List Operations
Accessing Elements
- Elements in a list can be accessed using square brackets with a position number (index) as follows:
- Note that indexing starts from 0.
- Attempting to access an index outside the list’s range will result in an
IndexError
.
- To access elements from the end of the list, you can use negative indices.
📚 Practice Problem
Create a list with values from 0.1
to 1.0
with 0.1
increments, and output the value 0.4
from the list.
(Try using both approaches: index from the beginning and index from the end.)
Modifying Elements
- List elements can be modified using assignment.
Getting Length
- To find the length of a list, use the
len
function.
Adding Elements (append
)
- To add an element to the end of a list, use the
append
method.- We will cover “methods” in more detail later, but simply put, they are “type-specific functions”.
You can use them by adding
.<method_name>
after a value of that type.
- We will cover “methods” in more detail later, but simply put, they are “type-specific functions”.
You can use them by adding
📚 Practice Problem
Add 5 sports names to the list sports
using the append
method and display the result.
List Methods
Finding Position (index
)
- The
index
method returns the position (index) of a specified value in the list.- If the specified value appears multiple times in the list, it returns the first index.
- If the specified value does not exist in the list, a
ValueError
is raised.
Counting Occurrences (count
)
- The
count
method returns the number of occurrences of a specified value in the list.
Removing Elements (remove
)
- The
remove
method removes the specified element from the list.
- If multiple occurrences of the value exist, only the first occurrence is removed.
Extracting Elements (pop
)
- The
pop
method extracts and returns the last element from the list.- The extracted element is removed from the list.
- If an index is specified as an argument to the
pop
method, it extracts and returns the element at that position.- The extracted element is removed from the list.
Inserting Elements (insert
)
- The
insert
method inserts a specified value at a specified index position.- This operation shifts all elements after the insertion point, which makes it slower compared to the
append
method when adding elements to the end. (For more details, see the column at the end)
- This operation shifts all elements after the insertion point, which makes it slower compared to the
Extending Lists (extend
)
- The
extend
method extends the list by appending elements from another list. (append
adds a single element, whileextend
adds all elements from the specified list)
- Adding lists with the addition operator (
+
) creates a new combined list. (Unlike theextend
method, the original list remains unchanged, and a new list is created)
📚 Practice Problem
Create the list from the previous problem using the extend
method instead of the append
method and display the result.
Column: Computational Complexity of List Operations
In programming, it’s important to be aware of the time (computational complexity) required for each operation.
Understanding the computational complexity of functions and methods can help improve performance, especially when dealing with large amounts of data.
Note on computational complexity notation:
O(1)
: Processing time is constant, regardless of input sizeO(n)
: In the worst case, processing time is proportional to the sizen
of the input data (list)O(k)
: In the worst case, processing time is proportional to the sizek
of the second input data (list)
Operation | Complexity | Description |
---|---|---|
list[i] (accessing elements) |
O(1) | Accessing elements by index is fast |
list[i] = x (modifying elements) |
O(1) | Modifying elements by index is fast |
len(list) |
O(1) | List size is internally maintained, so retrieval is fast |
append(x) |
O(1) (average) | Usually constant time, but occasionally O(n) due to memory reallocation |
index(x) |
O(n) | Needs to search from the beginning, potentially examining all elements in the worst case |
count(x) |
O(n) | Needs to scan the entire list to count matching elements |
remove(x) |
O(n) | Needs to search for the element from the beginning, then shift elements to fill the gap |
pop() (without args) |
O(1) | Removing from the end is fast |
pop(i) (with index) |
O(n) | After removing the element, all subsequent elements must be shifted left |
insert(i, x) |
O(n) | All elements after the insertion point must be shifted right, which is time-consuming |
extend(list2) |
O(k) | Time is proportional to the number of elements k in the list being added |
list1 + list2 |
O(n + k) | Creates a new list by copying both lists, so time is proportional to the total number of elements |