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

  • non-capturing group, Named Groups,groupdict()

    To create a non-capturing group in Python’s re module, you use the syntax (?:…). This groups a part of a regular expression together without creating a backreference for that group. A capturing group (…) saves the matched text. You can then access this captured text using methods like group(1), group(2), etc. A non-capturing group (?:…)…

  • Python Primitive Data Types & Functions: Explained with Examples

    1. Primitive Data Types Primitive data types are the most basic building blocks in Python. They represent simple, single values and are immutable (cannot be modified after creation). Key Primitive Data Types Type Description Example int Whole numbers (positive/negative) x = 10 float Decimal numbers y = 3.14 bool Boolean (True/False) is_valid = True str…

  • Alternation and Grouping

    Complete List of Alternation and Grouping in Python Regular Expressions Grouping Constructs Capturing Groups Pattern Description Example (…) Capturing group (abc) (?P<name>…) Named capturing group (?P<word>\w+) \1, \2, etc. Backreferences to groups (a)\1 matches “aa” (?P=name) Named backreference (?P<word>\w+) (?P=word) Non-Capturing Groups Pattern Description Example (?:…) Non-capturing group (?:abc)+ (?i:…) Case-insensitive group (?i:hello) (?s:…) DOTALL group (. matches…

  • Classes and Objects in Python

    Classes and Objects in Python What are Classes and Objects? In Python, classes and objects are fundamental concepts of object-oriented programming (OOP). Real-world Analogy Think of a class as a “cookie cutter” and objects as the “cookies” made from it. The cookie cutter defines the shape, and each cookie is an instance of that shape. 1. Using type() function The type() function returns…

  • Number Manipulation and F-Strings in Python, with examples:

    Python, mathematical operators are symbols that perform arithmetic operations on numerical values. Here’s a breakdown of the key operators: Basic Arithmetic Operators: Other Important Operators: Operator Precedence: Python follows the standard mathematical order of operations (often remembered by the acronym PEMDAS or BODMAS): Understanding these operators and their precedence is essential for performing calculations in…

  • 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,…

Leave a Reply

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