Using List in Python

Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Jun 2009
Posts: 13
Reputation: kes_ee is an unknown quantity at this point 
Solved Threads: 0
kes_ee kes_ee is offline Offline
Newbie Poster

Using List in Python

 
0
  #1
Jul 8th, 2009
What is the difference between these two methods;
  1. # Solution 1
  2. list1 = ['a', 'b', 'c', 'd']
  3. temp_list1 = list1
  4. for i in range(len(list1)):
  5. temp_list1[i] = "1"
  6. print list1; # ['1', '1', '1', '1']
  7. print temp_list1; # ['1', '1', '1', '1']
  8.  
  9. # Solution 2
  10. list2 = ['a', 'b', 'c', 'd']
  11. temp_list2 = list2
  12. temp_list2 = ['1', '1', '1', '1']
  13. print list2; # ['a', 'b', 'c', 'd']
  14. 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.
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 489
Reputation: shadwickman will become famous soon enough shadwickman will become famous soon enough 
Solved Threads: 76
shadwickman's Avatar
shadwickman shadwickman is offline Offline
Posting Pro in Training

Re: Using List in Python

 
0
  #2
Jul 8th, 2009
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
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 489
Reputation: shadwickman will become famous soon enough shadwickman will become famous soon enough 
Solved Threads: 76
shadwickman's Avatar
shadwickman shadwickman is offline Offline
Posting Pro in Training

Re: Using List in Python

 
0
  #3
Jul 8th, 2009
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
  1. list1 = ['a', 'b', 'c', 'd']
  2. list2 = [x for x in list1]
That'll copy all the indices of list1 into a new address for list2, and thus, they are independent of each other.
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
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 1,546
Reputation: Ene Uran has a spectacular aura about Ene Uran has a spectacular aura about 
Solved Threads: 174
Ene Uran's Avatar
Ene Uran Ene Uran is offline Offline
Posting Virtuoso

Re: Using List in Python

 
1
  #4
Jul 8th, 2009
Simply use list():
  1. list1 = ['a', 'b', 'c', 'd']
  2. # this will create a new list with a different mem ref
  3. temp_list1 = list(list1)
  4. for i in range(len(list1)):
  5. temp_list1[i] = "1"
  6. print list1; # ['a', 'b', 'c', 'd']
  7. print temp_list1; # ['1', '1', '1', '1']
For nested lists you need to use deepcopy() from module copy.
Last edited by Ene Uran; Jul 8th, 2009 at 11:07 am.
drink her pretty
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 4,040
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 933
Moderator
vegaseat's Avatar
vegaseat vegaseat is online now Online
DaniWeb's Hypocrite

Re: Using List in Python

 
0
  #5
Jul 8th, 2009
Here is an example for the use of deepcopy with a nested list. First without ...
  1. # a nested list
  2. list1 = ['a', 'b', 'c', 'd', [1, 2, 3]]
  3. list2 = [x for x in list1]
  4.  
  5. # now replace 2 with 77 in list1
  6. list1[4][1] = 77
  7.  
  8. print(list1) # ['a', 'b', 'c', 'd', [1, 77, 3]]
  9. print(list2) # ['a', 'b', 'c', 'd', [1, 77, 3]] oops!!!
now with ...
  1. import copy
  2.  
  3. # a nested list
  4. list1 = ['a', 'b', 'c', 'd', [1, 2, 3]]
  5. list2 = copy.deepcopy(list1)
  6.  
  7. # now replace 2 with 77 in list1
  8. list1[4][1] = 77
  9.  
  10. print(list1) # ['a', 'b', 'c', 'd', [1, 77, 3]]
  11. print(list2) # ['a', 'b', 'c', 'd', [1, 2, 3]] okay!!!
In the first example the list comprehension simply copied the inner list as an alias list which shares the memory reference.
Last edited by vegaseat; Jul 8th, 2009 at 7:35 pm.
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC