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

  • Home
  • SEARCH
  • 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 8593795
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T00:04:17+00:00 2026-06-12T00:04:17+00:00

I’m quite confused about the use of methods to manipulate a list in python.

  • 0

I’m quite confused about the use of methods to manipulate a list in python. Say you have

mylist = [1,2,3,4]
mylist.append(5) #works fine, 

but when I put

def adding(mylist):
    mylist.append(5)

print adding(mylist) #will print out the orginal list without the 5.

Also,

data2 = data3 = [1,2,3]
data2 = data2 +[4]+[5]+[6] # doesn't works
data3 += [4] +[5] +[6] #works

But I’m quite confused how they work.

So I want to make a list(not in terms of python) of different ways to manipulate a list. Can anyone help?

  • 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-12T00:04:19+00:00Added an answer on June 12, 2026 at 12:04 am

    I suspect the reason you expect the original code to work is because you are
    more familiar with passing by references in languages like C++. It is important
    then to be aware of the difference of pass by reference and pass by value.
    It is even more important to understand which one Python is using.

    In short, Python actually uses neither pass by reference or pass by value;
    passing is done via object in Python.


    Long answer:

    An example of pass by value can be made with C:

    #include <stdio.h>
    
    void adding(int i) {
        i = i + 1;
    }
    
    int main() {
        int i = 0;
        adding(i);
        printf("i is %d\n", i);  // will print "i is 0"
        return 0;
    }
    

    Here, the value of i is copied when passed to adding(). the int i inside
    adding(int i) resides in a different memory location than the original int
    i
    inside main(). So inside adding(), i = i + 1 only affects the value in
    the chunk of memory known only inside the scope of adding(), the i in
    main() is totally unaffected because it resides in a different chunk of
    memory.

    An example of pass by reference can be made with C++:

    #include <iostream>
    
    void adding(int &i) {
        i = i + 1;
    }
    
    int main() {
        int i = 0;
        adding(i);
        std::cout << "i is " << i << std::endl;  // will print "i is 1"
        return 0;
    }
    

    Here i is passed by reference to adding(). The i inside adding() refers
    to exactly the same chunk of memory as the i in main(). And hence, i will
    be incremented to 1.


    Now, let’s talk about Python by going back to your example (that I modified
    by guessing what you actually meant =]):

    # Case 1
    mylist = [1,2,3,4]
    mylist.append(5)  # will print [1,2,3,4,5]
    
    # Case 2
    mylist = [1,2,3,4]
    def adding(mylist):
        mylist.append(5)
    
    adding(mylist)
    print mylist  # will print [1,2,3,4,5]
    
    # Case 3
    mylist = [1,2,3,4]
    def adding(mylist):
        mylist = mylist + [5]
    
    adding(mylist)
    print mylist  # will print [1,2,3,4]
    
    # Case 4
    mylist = [1,2,3,4]
    def adding(mylist):
        mylist += [5]
    
    adding(mylist)
    print mylist  # will print [1,2,3,4,5]
    

    Here, mylist is passed neither by value or reference. It is in fact passed by
    object.

    In Python, mylist = [1,2,3,4] creates a list [1,2,3,4] and attach a
    tag
    mylist to it. The important thing here to note is that list is an
    object (IIRC, every object is a PyObject or an extension to it).

    Understand this distinction will enable you to make sense of the outputs in the
    example cases:

    1. In Case 1, .append() is called on [1,2,3,4] list object directly to
      alter the list value, and hence the object myobject is attaching to has
      changed. Notice that this is possible also because lists are mutable objects in
      Python.

    2. In Case 2, mylist is NOT copied on passing to adding(), it is still
      “attached” to the same list object, similar to Case 1. Hence, calling
      .append() on this object will alter the object itself. Outside of
      adding(), since mylist still attaches to the same object (even though the
      object has altered itself), we get the output [1,2,3,4,5].

    3. In Case 3, inside adding(), the local variable mylist is attached to
      a new object created by [1,2,3,4]+[4]. But the object the original mylist
      is pointing to has never been altered, therefore, the output remains to be
      [1,2,3,4].

    4. Case 4 is a bit tricky. In Python lists are mutable, += acts
      as a shortcut like .extend(), which acts on the calling object itself. Hence,
      the original object has been modified to [1,2,3,4,5] and mylist still
      attaches to it. It is perhaps also worth noting that for immutable objects like
      tuples, strings and numbers, += will create a new object and attach the
      variable to it.


    As for different ways to use lists in Python, this article written by Fredrik Lundh contains very thorough information.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I don't have much knowledge about the IPv6 protocol, so sorry if the question
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I am trying to understand how to use SyndicationItem to display feed which is
I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,
I am reading a book about Javascript and jQuery and using one of the
I have this code to decode numeric html entities to the UTF8 equivalent character.

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.