Python
Python Basics
- Introduction to Python and Its History
- Python Syntax and Indentation
- Python Variables and Data Types
- Dynamic and Strong Typing
- Comments and Docstrings
- Taking User Input (input())
- Printing Output (print())
- Python Operators (Arithmetic, Logical, Comparison)
- Type Conversion and Casting
- Escape Characters and Raw Strings
Data Structures in Python
- Lists
- Dictionaries
- Dictionary Comprehensions
- Strings and String Manipulation
- Tuples
- Python Sets: Unordered Collections
- List Comprehensions and Generator Expressions
- Set Comprehensions
- String Formatting
- Indexing and Slicing
Control Flow and Loops
- Conditional Statements: if, elif, and else
- Loops and Iteration
- While Loops
- Nested Loops
- Loop Control Statements
- Iterators and Iterables
- List, Dictionary, and Set Iterations
Functions and Scope
- Defining and Calling Functions (`def`)
- Function Arguments (`*args`, `**kwargs`)
- Default Arguments and Keyword Arguments
- Lambda Functions
- Global and Local Scope
- Function Return Values
- Recursion in Python
Object-Oriented Programming (OOP)
- Object-Oriented Programming
- Classes and Objects
- the `__init__()` Constructor
- Instance Variables and Methods
- Class Variables and `@classmethod`
- Encapsulation and Data Hiding
- Inheritance and Subclasses
- Method Overriding and super()
- Polymorphism
- Magic Methods and Operator Overloading
- Static Methods
- Abstract Classes and Interfaces
Python Programs
- AES-256 Encryption
- Array : Find median in an integer array
- Array : Find middle element in an integer array
- Array : Find out the duplicate in an array
- Array : Find print all subsets in an integer array
- Program : Array : Finding missing number between from 1 to n
- Array : Gap and Island problem
- Python Program stock max profit
- Reverse words in Python
- Python array duplicate program
- Coin change problem in python
- Python Write fibonacci series program
- Array : find all the pairs whose sum is equal to a given number
- Find smallest and largest number in array
- Iterate collections
- List comprehensions
- Program: Calculate Pi in Python
- String Formatting in Python
Lists and List Methods (list) in Python
When you start learning Python, one of the most essential and versatile data structures you’ll encounter is the list. Lists are used to store multiple values in a single variable. Whether you’re keeping track of student names, storing numbers, or managing items in a to-do app, Python lists offer a flexible and powerful solution.
This article will take you from the basics of list creation to mastering built-in list methods — all explained with simple language and practical examples.
Why Are Lists Important in Python?
Lists are fundamental because:
- Versatility: They can store mixed data types — strings, numbers, or even other lists.
- Mutable: Lists can be changed after creation — items can be added, removed, or modified.
- Indexed: Like strings, lists allow access to individual items through indexing.
- Widely Used: From loops to conditionals to real-world applications like sorting algorithms, lists are everywhere in Python programming.
Prerequisites
To get the most out of this guide, you should:
- Have Python installed (any version from 3.x is fine).
- Know how to run Python in a script or an interpreter.
- Understand basic variables and data types like integers and strings.
What Will This Guide Cover?
This comprehensive guide includes:
- What is a list?
- How to create a list
- Accessing list items (indexing and slicing)
- Modifying lists
- Common list methods
- Nested lists
- List comprehensions
- Practical examples
1. What Is a List in Python?
A list is a collection of items enclosed in square brackets ([]), and items are separated by commas.
# Examplesnumbers = [1, 2, 3, 4]fruits = ["apple", "banana", "cherry"]mixed = [1, "hello", True, 3.5]2. Creating a List
You can create a list using:
empty_list = []fruits = list(["apple", "banana", "cherry"])Lists can also be built dynamically:
my_list = []my_list.append("Python")3. Accessing List Items (Indexing and Slicing)
Indexing
Like strings, list elements are indexed starting from 0.
fruits = ["apple", "banana", "cherry"]print(fruits[0]) # appleprint(fruits[-1]) # cherry (last item)Slicing
print(fruits[0:2]) # ['apple', 'banana']print(fruits[1:]) # ['banana', 'cherry']4. Modifying Lists
Changing Values
fruits[1] = "orange"print(fruits) # ['apple', 'orange', 'cherry']Adding Items
fruits.append("mango")fruits.insert(1, "grape")Removing Items
fruits.remove("apple")del fruits[0]popped = fruits.pop() # removes last item5. Common List Methods
Python lists come with many built-in methods:
| Method | Description |
|---|---|
append(x) | Adds an element to the end |
insert(i, x) | Inserts at a specific position |
remove(x) | Removes the first occurrence of x |
pop(i) | Removes and returns item at i |
clear() | Removes all elements |
index(x) | Returns the first index of x |
count(x) | Counts how many times x occurs |
sort() | Sorts the list |
reverse() | Reverses the list |
copy() | Returns a shallow copy |
Examples
numbers = [5, 3, 8, 6]
numbers.append(10)numbers.sort()numbers.reverse()print(numbers)6. Nested Lists
A list can contain other lists.
matrix = [ [1, 2, 3], [4, 5, 6], [7, 8, 9]]print(matrix[0][1]) # Output: 27. List Comprehension
A compact way to create lists.
squares = [x**2 for x in range(5)] # [0, 1, 4, 9, 16]With condition
even = [x for x in range(10) if x % 2 == 0]8. Practical Examples
Example 1: Remove Duplicates
my_list = [1, 2, 2, 3, 4, 4, 5]unique = list(set(my_list))print(unique)Example 2: Sum of List Items
numbers = [10, 20, 30]print(sum(numbers)) # Output: 60Example 3: Find Max/Min
print(max(numbers)) # 30print(min(numbers)) # 10Example 4: Flatten Nested Lists
nested = [[1, 2], [3, 4], [5]]flat = [item for sublist in nested for item in sublist]print(flat) # [1, 2, 3, 4, 5]Example 5: Filter Strings from List
data = [1, "hello", 3.5, "world"]strings = [x for x in data if type(x) == str]print(strings) # ['hello', 'world']Python lists are among the most versatile tools in your coding toolkit. They’re dynamic, easy to manipulate, and powerful enough to manage large datasets or simple to-do items. Learning how to use lists effectively is a stepping stone toward writing clean, efficient, and scalable Python code.
By mastering list creation, slicing, updating, and using built-in methods, you’ll be well-equipped to solve a wide range of programming challenges — from basic automation to advanced algorithm design.