943,769 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Marked Solved
  • Views: 489
  • Python RSS
Jul 8th, 2009
0

Using List in Python

Expand Post »
What is the difference between these two methods;
python Syntax (Toggle Plain Text)
  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.
Similar Threads
Reputation Points: 10
Solved Threads: 1
Newbie Poster
kes_ee is offline Offline
16 posts
since Jun 2009
Jul 8th, 2009
0

Re: Using List in Python

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.
Reputation Points: 186
Solved Threads: 77
Posting Pro in Training
shadwickman is offline Offline
495 posts
since Jul 2007
Jul 8th, 2009
0

Re: Using List in Python

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
python Syntax (Toggle Plain Text)
  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.
Reputation Points: 186
Solved Threads: 77
Posting Pro in Training
shadwickman is offline Offline
495 posts
since Jul 2007
Jul 8th, 2009
1

Re: Using List in Python

Simply use list():
python Syntax (Toggle Plain Text)
  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.
Reputation Points: 625
Solved Threads: 211
Posting Virtuoso
Ene Uran is offline Offline
1,704 posts
since Aug 2005
Jul 8th, 2009
0

Re: Using List in Python

Here is an example for the use of deepcopy with a nested list. First without ...
python Syntax (Toggle Plain Text)
  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 ...
python Syntax (Toggle Plain Text)
  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.
Moderator
Reputation Points: 1333
Solved Threads: 1403
DaniWeb's Hypocrite
vegaseat is offline Offline
5,792 posts
since Oct 2004

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: Guidelines to Original Posters and respondents
Next Thread in Python Forum Timeline: [newbie question] Loading images? [Python-Pygame]





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC