append(), extend(), and insert() methods in Python lists

append()extend(), and insert() methods in Python lists, along with slicing where applicable.


1. append() Method

Adds a single element to the end of the list.

Examples:

  1. Adding an integer:pythonnumbers = [1, 2, 3] numbers.append(4) print(numbers) # Output: [1, 2, 3, 4]
  2. Adding a string:pythonfruits = [“apple”, “banana”] fruits.append(“cherry”) print(fruits) # Output: [“apple”, “banana”, “cherry”]
  3. Adding a list (as a single element):pythonlist1 = [1, 2] list1.append([3, 4]) print(list1) # Output: [1, 2, [3, 4]]
  4. Using slicing + append:pythonnums = [10, 20, 30] nums[len(nums):] = [40] # Same as append(40) print(nums) # Output: [10, 20, 30, 40]

2. extend() Method

Adds multiple elements (iterable items) to the end of the list.

Examples:

  1. Extending with a list:pythonnums = [1, 2] nums.extend([3, 4]) print(nums) # Output: [1, 2, 3, 4]
  2. Extending with a string (each character is added):pythonletters = [‘a’, ‘b’] letters.extend(“cd”) print(letters) # Output: [‘a’, ‘b’, ‘c’, ‘d’]
  3. Extending with a tuple:pythonitems = [1, 2] items.extend((3, 4)) print(items) # Output: [1, 2, 3, 4]
  4. Using slicing + extend:pythonlist1 = [1, 2] list1[len(list1):] = [3, 4] # Same as extend([3, 4]) print(list1) # Output: [1, 2, 3, 4]

3. insert() Method

Inserts an element at a specific position.

Examples:

  1. Inserting at index 0:pythonnums = [2, 3] nums.insert(0, 1) # Insert 1 at the beginning print(nums) # Output: [1, 2, 3]
  2. Inserting in the middle:pythonfruits = [“apple”, “cherry”] fruits.insert(1, “banana”) print(fruits) # Output: [“apple”, “banana”, “cherry”]
  3. Inserting at the end (like append):pythonnums = [1, 2] nums.insert(2, 3) # Same as append(3) print(nums) # Output: [1, 2, 3]
  4. Using slicing + insert:pythonlist1 = [10, 30] list1[1:1] = [20] # Inserts 20 at index 1 print(list1) # Output: [10, 20, 30]

Key Differences:

MethodModifies List?Adds Single/Multiple Elements?Position
append()✅ YesSingle element (even if it’s a list)End
extend()✅ YesMultiple elements (iterable)End
insert()✅ YesSingle elementSpecified index

Similar Posts

  • replace(), join(), split(), rsplit(), and splitlines() methods in Python

    1. replace() Method Purpose: Replaces occurrences of a substring with another substring.Syntax: python string.replace(old, new[, count]) Examples: Example 1: Basic Replacement python text = “Hello World” new_text = text.replace(“World”, “Python”) print(new_text) # Output: “Hello Python” Example 2: Limiting Replacements (count) python text = “apple apple apple” new_text = text.replace(“apple”, “orange”, 2) print(new_text) # Output: “orange orange apple”…

  • math Module

    The math module in Python is a built-in module that provides access to standard mathematical functions and constants. It’s designed for use with complex mathematical operations that aren’t natively available with Python’s basic arithmetic operators (+, -, *, /). Key Features of the math Module The math module covers a wide range of mathematical categories,…

  • Generators in Python

    Generators in Python What is a Generator? A generator is a special type of iterator that allows you to iterate over a sequence of values without storing them all in memory at once. Generators generate values on-the-fly (lazy evaluation) using the yield keyword. Key Characteristics Basic Syntax python def generator_function(): yield value1 yield value2 yield value3 Simple Examples Example…

  • Random Module?

    What is the Random Module? The random module in Python is used to generate pseudo-random numbers. It’s perfect for: Random Module Methods with Examples 1. random() – Random float between 0.0 and 1.0 Generates a random floating-point number between 0.0 (inclusive) and 1.0 (exclusive). python import random # Example 1: Basic random float print(random.random()) # Output: 0.5488135079477204 # Example…

  • Inheritance in OOP Python: Rectangle & Cuboid Example

    Rectangle Inheritance in OOP Python: Rectangle & Cuboid Example Inheritance in object-oriented programming (OOP) allows a new class (the child class) to inherit properties and methods from an existing class (the parent class). This is a powerful concept for code reusability ♻️ and establishing a logical “is-a” relationship between classes. For instance, a Cuboid is…

Leave a Reply

Your email address will not be published. Required fields are marked *