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

  • ASCII ,Uni Code Related Functions in Python

    ASCII Code and Related Functions in Python ASCII (American Standard Code for Information Interchange) is a character encoding standard that assigns numerical values to letters, digits, punctuation marks, and other characters. Here’s an explanation of ASCII and Python functions that work with it. ASCII Basics Python Functions for ASCII 1. ord() – Get ASCII value of a…

  • Currency Converter

    Challenge: Currency Converter Class with Accessors & Mutators Objective: Create a CurrencyConverter class that converts an amount from a foreign currency to your local currency, using accessor and mutator methods. 1. Class Properties (Instance Variables) 2. Class Methods 3. Task Instructions

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

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

  • Polymorphism

    Polymorphism is a core concept in OOP that means “many forms” 🐍. In Python, it allows objects of different classes to be treated as objects of a common superclass. This means you can use a single function or method to work with different data types, as long as they implement a specific action. 🌀 Polymorphism…

  • Password Strength Checker

    python Enhanced Password Strength Checker python import re def is_strong(password): “”” Check if a password is strong based on multiple criteria. Returns (is_valid, message) tuple. “”” # Define criteria and error messages criteria = [ { ‘check’: len(password) >= 8, ‘message’: “at least 8 characters” }, { ‘check’: bool(re.search(r'[A-Z]’, password)), ‘message’: “one uppercase letter (A-Z)”…

Leave a Reply

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