| | |
Using List in Python
Thread Solved |
•
•
Join Date: Jun 2009
Posts: 13
Reputation:
Solved Threads: 0
What is the difference between these two methods;
By using solution1, want to update the temp_list1 with some other value at the same time want to retain the old value in list1. But this updated the both lists.
python Syntax (Toggle Plain Text)
# Solution 1 list1 = ['a', 'b', 'c', 'd'] temp_list1 = list1 for i in range(len(list1)): temp_list1[i] = "1" print list1; # ['1', '1', '1', '1'] print temp_list1; # ['1', '1', '1', '1'] # Solution 2 list2 = ['a', 'b', 'c', 'd'] temp_list2 = list2 temp_list2 = ['1', '1', '1', '1'] print list2; # ['a', 'b', 'c', 'd'] print temp_list2; # ['1', '1', '1', '1']
By using solution1, want to update the temp_list1 with some other value at the same time want to retain the old value in list1. But this updated the both lists.
Because your temporary list is the same thing as your other list. You said
temp_list1 = list1 . This means that the value for temp_list1 will be the same address in memory as the value for list1, i.e. they reference the same thing. Modifying one modifies the other because they are just 2 different names for the same data. "Two good old boys in a fire-apple red convertible. Stoned. Ripped. Twisted. Good people."
- Hunter S. Thompson
my photography
- Hunter S. Thompson
my photography
I forgot to mention that in solution #2, you assign temp_list2 to the same address of memory as list2, but then that's undone as you create a new list (and new address in memory for it) consisting of four ones. Therefore, temp_list2 no longer refers to the same data as list2.
If you want to copy the list over without making the new name reference the same data, how about
That'll copy all the indices of list1 into a new address for list2, and thus, they are independent of each other.
If you want to copy the list over without making the new name reference the same data, how about
python Syntax (Toggle Plain Text)
list1 = ['a', 'b', 'c', 'd'] list2 = [x for x in list1]
Last edited by shadwickman; Jul 8th, 2009 at 6:17 am.
"Two good old boys in a fire-apple red convertible. Stoned. Ripped. Twisted. Good people."
- Hunter S. Thompson
my photography
- Hunter S. Thompson
my photography
Simply use list():
For nested lists you need to use deepcopy() from module copy.
python Syntax (Toggle Plain Text)
list1 = ['a', 'b', 'c', 'd'] # this will create a new list with a different mem ref temp_list1 = list(list1) for i in range(len(list1)): temp_list1[i] = "1" print list1; # ['a', 'b', 'c', 'd'] print temp_list1; # ['1', '1', '1', '1']
Last edited by Ene Uran; Jul 8th, 2009 at 11:07 am.
drink her pretty
Here is an example for the use of deepcopy with a nested list. First without ...
now with ...
In the first example the list comprehension simply copied the inner list as an alias list which shares the memory reference.
python Syntax (Toggle Plain Text)
# a nested list list1 = ['a', 'b', 'c', 'd', [1, 2, 3]] list2 = [x for x in list1] # now replace 2 with 77 in list1 list1[4][1] = 77 print(list1) # ['a', 'b', 'c', 'd', [1, 77, 3]] print(list2) # ['a', 'b', 'c', 'd', [1, 77, 3]] oops!!!
python Syntax (Toggle Plain Text)
import copy # a nested list list1 = ['a', 'b', 'c', 'd', [1, 2, 3]] list2 = copy.deepcopy(list1) # now replace 2 with 77 in list1 list1[4][1] = 77 print(list1) # ['a', 'b', 'c', 'd', [1, 77, 3]] print(list2) # ['a', 'b', 'c', 'd', [1, 2, 3]] okay!!!
Last edited by vegaseat; Jul 8th, 2009 at 7:35 pm.
May 'the Google' be with you!
![]() |
Similar Threads
- Starting Python (Python)
- sort a list with python (Python)
- Tuple List (Python)
- Sorting in Python (Python)
- Limiting the size of List (Python)
- Is there any place where you can find a complete list of python commands? (Python)
Other Threads in the Python Forum
- Previous Thread: Guidelines to Original Posters and respondents
- Next Thread: [newbie question] Loading images? [Python-Pygame]
| Thread Tools | Search this Thread |
advanced aliased bash beginner bits calling casino changecolor clear command convert corners count csv cturtle cursor def definedlines dictionary digital dynamic dynamically event events examples external file float format frange function google gui hints homework i/o iframe import info input jaunty java line linux list lists loop matching mouse multiple number numbers obexftp output parsing path port prime programming projects py py2exe pygame pygtk python random rational raw_input recursion return scrolledtext signal singleton skinning stderr string strings subprocess table tails terminal text thread threading time tkinter tlapse tuple tutorial ubuntu unicode urllib urllib2 valueerror variable voip web-scrape whileloop word wxpython






