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

  • Global And Local Variables

    Global Variables In Python, a global variable is a variable that is accessible throughout the entire program. It is defined outside of any function or class. This means its scope is the entire file, and any function can access and modify its value. You can use the global keyword inside a function to modify a…

  • Python timedelta Explained

    Python timedelta Explained timedelta is a class in Python’s datetime module that represents a duration – the difference between two dates or times. It’s incredibly useful for date and time arithmetic. Importing timedelta python from datetime import timedelta, datetime, date Basic Syntax python timedelta(days=0, seconds=0, microseconds=0, milliseconds=0, minutes=0, hours=0, weeks=0) Examples 1. Basic timedelta Creation python from datetime…

  • re.subn()

    Python re.subn() Method Explained The re.subn() method is similar to re.sub() but with one key difference: it returns a tuple containing both the modified string and the number of substitutions made. This is useful when you need to know how many replacements occurred. Syntax python re.subn(pattern, repl, string, count=0, flags=0) Returns: (modified_string, number_of_substitutions) Example 1: Basic Usage with Count Tracking python import re…

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

  • What are Variables

    A program is essentially a set of instructions that tells a computer what to do. Just like a recipe guides a chef, a program guides a computer to perform specific tasks—whether it’s calculating numbers, playing a song, displaying a website, or running a game. Programs are written in programming languages like Python, Java, or C++,…

  • Python Variables: A Complete Guide with Interview Q&A

    Here’s a detailed set of notes on Python variables that you can use to explain the concept to your students. These notes are structured to make it easy for beginners to understand. Python Variables: Notes for Students 1. What is a Variable? 2. Rules for Naming Variables Python has specific rules for naming variables: 3….

Leave a Reply

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