in Education by
I have two list that I combine into a single list. Later I want to make a copy of this list so I can do some manipulation of this second list without affecting my first list. My understanding is if you use this [:] it should unlink the list and make a second copy of the list in a independent memory location. My problem is Im not see that work for this scenario I have. I have also tried using list command as well, but the result was the same. a = ['a','b','c'] b = ['1','2','3'] c = [a[:],b[:]] # list of list d = c[:] # want to create copy of the list of list so I can remove last item for item in d: del item[-1] # this is what I am getting returned. In [286]: c Out[286]: [['a', 'b'], ['1', '2']] In [287]: d Out[287]: [['a', 'b'], ['1', '2']] JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
[:] only makes a shallow copy. That is, it copies the list itself but not the items in the list. You should use copy.deepcopy. import copy d = copy.deepcopy(c) Think of a list as having pointers to objects. Now your variable is a pointer to the list. What [:] does is create an entirely new list with the same exact pointers. copy.deepcopy copies all attributes/references within your object, and the attributes/references within those attributes/references.

Related questions

0 votes
    When I executes my python program it shows the error TypeError: 'list' object is not callable. In these two lines ... ))).append(words) Select the correct answer from above options...
asked Jan 22, 2022 in Education by JackTerrance
0 votes
    I could swear I've seen the function (or method) that takes a list, like this [3, 7, 19] ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 17, 2022 in Education by JackTerrance
0 votes
    I could swear I've seen the function (or method) that takes a list, like this [3, 7, 19] ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 17, 2022 in Education by JackTerrance
0 votes
    How can I do the following in Python? array = [0, 10, 20, 40] for (i = array.length() - 1; i >= ... but from the end to the beginning. Select the correct answer from above options...
asked Feb 4, 2022 in Education by JackTerrance
0 votes
    Is this the cleanest way to write a list to a file, since writelines() doesn't insert newline characters? file. ... be a standard way. Select the correct answer from above options...
asked Feb 4, 2022 in Education by JackTerrance
0 votes
    I have some data either in a list of lists or a list of tuples, like this: data = [[1,2,3], [4,5, ... store tuples or lists in my list? Select the correct answer from above options...
asked Feb 4, 2022 in Education by JackTerrance
0 votes
    In Python 2.7, I could get dictionary keys, values, or items as a list: >>> newdict = {1:0, 2:0, 3:0} ... to return a list in Python 3? Select the correct answer from above options...
asked Feb 4, 2022 in Education by JackTerrance
0 votes
    I would like to get a list of Python modules, which are in my Python installation (UNIX server). How can ... in your computer? Select the correct answer from above options...
asked Jan 30, 2022 in Education by JackTerrance
0 votes
    I want to create a new list from the existing two lists that will have all the matches from list 2 which has an exact ... *", i) in a] Select the correct answer from above options...
asked Jan 25, 2022 in Education by JackTerrance
0 votes
    How can I get the number of elements of a list? Ex- items = [] items.append("one") items.append("two") items.append("three") Select the correct answer from above options...
asked Jan 24, 2022 in Education by JackTerrance
0 votes
    In a list with range(1,16) and I am trying to divide this list in some fixed numbe n . Let's assume n=7 ... uneven, How can I do that? Select the correct answer from above options...
asked Jan 23, 2022 in Education by JackTerrance
0 votes
    Can the Python list comprehension syntax be used to create dictionaries? For an example: By iterating over pairs of ... #doesn't work Select the correct answer from above options...
asked Jan 22, 2022 in Education by JackTerrance
0 votes
    How to remove an element from a list by index in Python, I tried list.remove method but it search the list and ... How can I do this? Select the correct answer from above options...
asked Jan 22, 2022 in Education by JackTerrance
0 votes
    I am confused about the usage of string.join(list) instead of list.join(string), for instance see this code: ... reason behind this? Select the correct answer from above options...
asked Jan 21, 2022 in Education by JackTerrance
0 votes
    How can someone get a index (1) in Python for a list ["qwe", "asd", "zxc"] and an item "asd" in the list? Select the correct answer from above options...
asked Jan 21, 2022 in Education by JackTerrance
...