Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9162561
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T14:14:24+00:00 2026-06-17T14:14:24+00:00

Possible Duplicate: Python passing list as argument I looked for many topics about that,

  • 0

Possible Duplicate:
Python passing list as argument

I looked for many topics about that, but I can’t understand what happen really.

I have this code:

def altera(L1, L2):
    for elemento in L2:
        L1.append(elemento)
    L2 = L2 + [4]
    L1[-1] = 10
    del L2[0]
    return L2[:]

Lista1 = [1,2,3]
Lista2 = [1,2,3]

Lista3 = altera(Lista1, Lista2)

print Lista1
print Lista2
print Lista3

and the result is:

[1, 2, 3, 1, 2, 10]
[1, 2, 3]
[2, 3, 4]

I can’t understand how the Lista1 was modified and Lista2 not. However before test the code, I thought that Lista1 and Lista2 would stay unmodified because they are global variables.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T14:14:25+00:00Added an answer on June 17, 2026 at 2:14 pm

    When you do L1.append(elemento) you are calling a method that actually changes the list named by the variable L1. All the other commands setting the values of L1 and L2 are actually just creating new names for new variables.

    This version doesn’t change anything:

    def altera(L1, L2):
        for elemento in L2:
            # create a new list and assign name L1
            L1 = L1 + [elemento]
        # create a new list and assign name L2
        L2 = L2 + [4]
        return L2
    
    Lista1 = [1,2,3]
    Lista2 = [1,2,3]
    
    Lista3 = altera(Lista1, Lista2)
    
    print Lista1
    print Lista2
    print Lista3
    

    While this one does:

    def altera(L1, L2):
        for elemento in L2:
            # Call method on L1 that changes it
            L1.append(elemento)
        # Call method on L2 that changes it
        L2.append(4)
        # Change object pointed to by name L1 -- Lista1
        L1[-1] = 10
        # Change object pointed to by name L2 -- Lista2
        del L2[0]
        return L2[:]
    
    Lista1 = [1,2,3]
    Lista2 = [1,2,3]
    
    Lista3 = altera(Lista1, Lista2)
    
    print Lista1
    print Lista2
    print Lista3
    

    However there is a tricky matter with L += [2] which is not exactly the same as L = L + 2. The Python Language Reference section on Augmented assignment statements explains the difference:

    An augmented assignment expression like x += 1 can be rewritten as x = x + 1 to achieve a similar, but not exactly equal effect. In the augmented version, x is only evaluated once. Also, when possible, the actual operation is performed in-place, meaning that rather than creating a new object and assigning that to the target, the old object is modified instead.”

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Python “extend” for a dictionary I know that Python list can be
Possible Duplicate: Python list confusion I've got one little question about Python lists: Why
Possible Duplicate: Python list subtraction operation In Python you can concatenate lists like so:
Possible Duplicate: Python list problem I don't understand behavior of lists in python: >>>
Possible Duplicate: Python : how to append new elements in a list of list?
Possible Duplicate: Python: What is the best way to check if a list is
Possible Duplicate: Python, compute list difference I have two lists For example: A =
Possible Duplicate: Python code to pick out all possible combinations from a list? I
Possible Duplicate: How do you split a list into evenly sized chunks in Python?
Possible Duplicate: Passing meta-characters to Python as arguments from command line I want to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.