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

  • Demo And course Content

    What is Python? Python is a high-level, interpreted, and general-purpose programming language known for its simplicity and readability. It supports multiple programming paradigms, including: Python’s design philosophy emphasizes code readability (using indentation instead of braces) and developer productivity. History of Python Fun Fact: Python is named after Monty Python’s Flying Circus (a British comedy show), not the snake! 🐍🎭 Top Career Paths After Learning Core Python 🐍…

  • Python Functions

    A function is a block of organized, reusable code that is used to perform a single, related action. Functions provide better modularity for your application and a high degree of code reusing. Defining a Function In Python, a function is defined using the def keyword, followed by the function name, a set of parentheses (),…

  • Sets in Python

    Sets in Python A set in Python is an unordered collection of unique elements. Sets are mutable, meaning you can add or remove items, but the elements themselves must be immutable (like numbers, strings, or tuples). Key Characteristics of Sets: Different Ways to Create Sets in Python Here are various methods to create sets in…

  • Formatting Date and Time in Python

    Formatting Date and Time in Python Python provides powerful formatting options for dates and times using the strftime() method and parsing using strptime() method. 1. Basic Formatting with strftime() Date Formatting python from datetime import date, datetime # Current date today = date.today() print(“Date Formatting Examples:”) print(f”Default: {today}”) print(f”YYYY-MM-DD: {today.strftime(‘%Y-%m-%d’)}”) print(f”MM/DD/YYYY: {today.strftime(‘%m/%d/%Y’)}”) print(f”DD-MM-YYYY: {today.strftime(‘%d-%m-%Y’)}”) print(f”Full month: {today.strftime(‘%B %d, %Y’)}”) print(f”Abbr…

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

  • Vs code

    What is VS Code? 💻 Visual Studio Code (VS Code) is a free, lightweight, and powerful code editor developed by Microsoft. It supports multiple programming languages (Python, JavaScript, Java, etc.) with: VS Code is cross-platform (Windows, macOS, Linux) and widely used for web development, data science, and general programming. 🌐📊✍️ How to Install VS Code…

Leave a Reply

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