complete list of list methods in python

1. Adding Elements

  • append()
  • extend()
  • insert()

2. Removing Elements

  • remove()
  • pop()
  • clear()

3. Accessing Elements

  • index()
  • count()

4. Sorting & Reversing

  • sort()
  • reverse()

5. Copying & Joining

  • copy()

6. List Comparison & Operations

  • (No direct methods, but ==+* are used with lists)

Note:

  • These are the built-in methods for lists in Python (as of Python 3.x).
  • Some operations (like slicing, concatenation) use syntax rather than methods.

Python List copy() Method with Examples

The copy() method in Python returns a shallow copy of a list. This means it creates a new list with the same elements as the original, but the new list is a separate object in memory.

Key Points:

  • Creates a new list with the same elements
  • Modifications to the new list won’t affect the original
  • Shallow copy means nested objects are still references to the same objects

Examples:

Example 1: Basic copy

python

original = [1, 2, 3]
copied = original.copy()

print(copied)  # Output: [1, 2, 3]
print(original is copied)  # Output: False (they are different objects)

Example 2: Modifying the copy doesn’t affect original

python

fruits = ['apple', 'banana', 'cherry']
fruit_copy = fruits.copy()

fruit_copy.append('orange')
print(fruits)      # Output: ['apple', 'banana', 'cherry']
print(fruit_copy)   # Output: ['apple', 'banana', 'cherry', 'orange']

Example 3: Shallow copy demonstration (nested lists)

python

original = [[1, 2], [3, 4]]
copied = original.copy()

copied[0][0] = 99
print(original)  # Output: [[99, 2], [3, 4]] (nested list is shared)

Example 4: Copy vs assignment

python

a = [1, 2, 3]
b = a          # assignment (same object)
c = a.copy()   # copy (new object)

a.append(4)
print(b)  # Output: [1, 2, 3, 4] (b is same as a)
print(c)  # Output: [1, 2, 3]    (c is independent)

Example 5: Copying empty list

python

empty = []
empty_copy = empty.copy()

empty_copy.append(1)
print(empty)       # Output: []
print(empty_copy)  # Output: [1]

For deep copies (where nested objects are also copied), you would use the copy.deepcopy() function from the copy module instead.

Similar Posts

  • Variable Length Positional Arguments in Python

    Variable Length Positional Arguments in Python Variable length positional arguments allow a function to accept any number of positional arguments. This is done using the *args syntax. Syntax python def function_name(*args): # function body # args becomes a tuple containing all positional arguments Simple Examples Example 1: Basic *args python def print_numbers(*args): print(“Numbers received:”, args) print(“Type of…

  • Create a User-Defined Exception

    A user-defined exception in Python is a custom error class that you create to handle specific error conditions within your code. Instead of relying on built-in exceptions like ValueError, you define your own to make your code more readable and to provide more specific error messages. You create a user-defined exception by defining a new…

  • Predefined Character Classes

    Predefined Character Classes Pattern Description Equivalent . Matches any character except newline \d Matches any digit [0-9] \D Matches any non-digit [^0-9] \w Matches any word character [a-zA-Z0-9_] \W Matches any non-word character [^a-zA-Z0-9_] \s Matches any whitespace character [ \t\n\r\f\v] \S Matches any non-whitespace character [^ \t\n\r\f\v] 1. Literal Character a Matches: The exact character…

  • recursive function

    A recursive function is a function that calls itself to solve a problem. It works by breaking down a larger problem into smaller, identical subproblems until it reaches a base case, which is a condition that stops the recursion and provides a solution to the simplest subproblem. The two main components of a recursive function…

  • Python Statistics Module

    Python Statistics Module: Complete Methods Guide with Examples Here’s a detailed explanation of each method in the Python statistics module with 3 practical examples for each: 1. Measures of Central Tendency mean() – Arithmetic Average python import statistics as stats # Example 1: Basic mean calculation data1 = [1, 2, 3, 4, 5] result1 = stats.mean(data1) print(f”Mean of…

  • Instance Variables,methods

    Instance Variables Instance variables are variables defined within a class but outside of any method. They are unique to each instance (object) of a class. This means that if you create multiple objects from the same class, each object will have its own separate copy of the instance variables. They are used to store the…

Leave a Reply

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