Python Lists

📘 Section 1 — What is a List?

âš¡ 1. Definition

A list is a collection of items stored in a single variable. Lists are ordered and changeable (mutable).

🧠 2. Why Use Lists?

Instead of creating separate variables for each item, we can store them all in one list:

📘 Section 2 — Creating Lists

âš¡ 1. Basic Syntax

Lists are created using square brackets []

Example:

python
# Empty list my_list = [] # List of numbers numbers = [1, 2, 3, 4, 5] # List of strings fruits = ["apple", "banana", "cherry"] # Mixed types mixed = [1, "hello", 3.14, True]

🧮 2. Reading Lists from Input

When reading lists from terminal input, there are two common approaches:

Method 1: Reading as Strings (split)

Use split(" ") to convert a space-separated string into a list.

Example:

python
# Input: apple banana cherry fruits = input("Enter fruits separated by spaces: ").split(" ") print(fruits) # Output: ['apple', 'banana', 'cherry']

Method 2: Reading as Numbers (split + map)

Use split(" ") combined with map() to convert all elements to numbers.

Example:

python
# Input: 10 20 30 40 50 numbers = list(map(int, input("Enter numbers: ").split(" "))) print(numbers) # Output: [10, 20, 30, 40, 50] # How it works: # input().split(" ") → ['10', '20', '30', '40', '50'] (strings) # map(int, ...) → converts each string to integer # list(...) → converts the result to a list

📘 Section 3 — Accessing Elements

âš¡ 1. Index

Each element has an index (position). Indexing starts at 0.

Example:

python
fruits = ["Apple", "Banana", "Cherry"] print(fruits[0]) # First element - "Apple" print(fruits[1]) # Second element - "Banana" print(fruits[2]) # Third element - "Cherry"

📘 Section 4 — Modifying Lists

âš¡ 1. Change Value

Lists are mutable — you can change their values.

Example:

python
colors = ["Red", "Green", "Blue"] colors[1] = "Yellow" # Change Green to Yellow print(colors) # Output: ['Red', 'Yellow', 'Blue']

📘 Section 5 — List Methods

âš¡ 1. append() - Add to End

Add an element to the end of the list.

Example:

python
animals = ["Dog", "Cat"] animals.append("Bird") print(animals) # Output: ['Dog', 'Cat', 'Bird']

🧮 2. remove() - Delete Item

Remove the first occurrence of a value.

Example:

python
items = ["Book", "Pen", "Pencil"] items.remove("Pen") print(items) # Output: ['Book', 'Pencil']

∑ 3. len() - Get Length

Return the number of elements in the list.

Example:

python
grades = [85, 90, 78, 92] print(len(grades)) # Output: 4

📊 4. sort() - Sort List

Sort the list in ascending order.

Example:

python
scores = [45, 12, 89, 33] scores.sort() print(scores) # Output: [12, 33, 45, 89]

📘 Section 6 — The "in" Operator

âš¡ 1. Check Existence

Use the in operator to check if an item exists in a list. It returns True or False.

Example:

python
menu = ["Pizza", "Burger", "Salad"] print("Burger" in menu) # Output: True print("Sushi" in menu) # Output: False print("Pizza" not in menu) # Output: False

📘 Section 7 — Slicing

âš¡ 1. Basic Slicing

Extract a portion of the list using list[start:stop]

Example:

python
letters = ['a', 'b', 'c', 'd', 'e'] print(letters[1:4]) # Elements from index 1 to 3: ['b', 'c', 'd']

🧮 2. Slicing from Start

Example:

python
letters = ['a', 'b', 'c', 'd', 'e'] print(letters[:3]) # First 3 elements: ['a', 'b', 'c']

📘 Section 8 — Practice Exercises

🔢 Exercise 1: Access by Index

Input a list of numbers (space-separated) and an index, then print the value at that index.

Example Input:

10 20 30 40 50
2

Example Output:

30

🔢 Exercise 2: Count Equal to Last

Input a list of numbers and count how many numbers are equal to the last element.

Example Input:

5 3 5 7 5 5

Example Output:

4

🔢 Exercise 3: Elements Greater than Average

Input a list of numbers, calculate the average, then print all elements greater than the average.

Example Input:

10 20 30 40 50

Example Output:

Average: 30.0 Elements greater than average: 40 50

🔢 Exercise 4: Find Maximum and Its Position

Input a list of numbers, find the maximum value, and print its index (position).

Example Input:

15 42 8 23

Example Output:

Maximum value: 42 Index: 1

🔢 Exercise 5: Reverse the List

Input a list of numbers and print the list in reversed order.

Example Input:

1 2 3 4 5

Example Output:

5 4 3 2 1

🔢 Exercise 6: Count Occurrences

Input a list of numbers and a target value, then count how many times the target appears.

Example Input:

3 5 3 7 3 8 3
3

Example Output:

4

🔢 Exercise 7: Even and Odd Separation

Input a list of numbers, create separate lists for even and odd numbers, then print both.

Example Input:

12 7 8 15 4 9

Example Output:

Even: [12, 8, 4] Odd: [7, 15, 9]

🔢 Exercise 8: Remove Duplicates

Input a list of numbers and create a new list with only unique values (no duplicates).

Example Input:

5 3 5 7 3 8 5

Example Output:

Unique: [5, 3, 7, 8]

🔢 Exercise 9: Sum of Two Adjacent Elements

Input a list of numbers and create a new list where each element is the sum of two adjacent elements.

Example Input:

10 20 30 40

Example Output:

[30, 50, 70]

🔢 Exercise 10: Find Second Largest

Input a list of numbers (N ≥ 2) and print the second largest number.

Example Input:

45 23 67 12 89

Example Output:

67

Summary

Lists are one of the most important data structures in Python. Practice these concepts regularly!