3

Bumped into a programming issue that puzzles me a bit. Im parsing data and:

  • add each relevant chunk to list1, append list1 to final_list. (for easy and neat future presentation)
  • For next relevant piece of information I'm deleting the "old" information in list1 with *list1[:] = []
  • append the new fresh information to list1, append list1 to final_list and voilà... I thought :)

All help appreciated!

Example:

final_list=[]
list1 = [1,2,3,4]
list2 = [5,6,7,8]

final_list.append(list1)
final_list.append(list2)
print final_list

list1[:] = []
print final_list

Example output

[[1, 2, 3, 4], [5, 6, 7, 8]]
[[], [5, 6, 7, 8]]

Sys.version

Python: 2.7.9 (default, Dec 10 2014, 12:24:55) [MSC v.1500 32 bit (Intel)]
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Fredrik
  • 63
  • 5
  • Hello and welcome to StackOverflow. Please take some time to read the help page, especially the sections named ["What topics can I ask about here?"](http://stackoverflow.com/help/on-topic) and ["What types of questions should I avoid asking?"](http://stackoverflow.com/help/dont-ask). And more importantly, please read [the Stack Overflow question checklist](http://meta.stackexchange.com/q/156810/204922). You might also want to learn about [Minimal, Complete, and Verifiable Examples](http://stackoverflow.com/help/mcve). – Morgan Thrapp Sep 18 '15 at 13:26
  • W hat did you expect to happen? – Karoly Horvath Sep 18 '15 at 13:42
  • I'm not sure why this surprises you. Can you explain what you expect to see? – Daniel Roseman Sep 18 '15 at 13:42
  • Hi, what i want to accheive essentially is to create a list of lists. Keep adding data to final_list from within a loop. – Fredrik Sep 18 '15 at 13:45
  • @Fredrik, is you say something is not as you expected, you should say what you expected or what you find confusing. In this case, I think what you find confusing is that list1 has also changed in the final_list, right? In any case, this is a very common problem when you start working with python. The final_list contains two "labels" that point towards list1 and list2, to WHATEVER you put there. If you change list1 or list2, final_list is changed accordingly – Jblasco Sep 18 '15 at 13:45
  • Why did you empty one of the lists then? – Daniel Roseman Sep 18 '15 at 13:46
  • Tanks for the clarification Jblasco. Its clear that its a flaw with my original idea of how to add multiple rows of values to final_list for easy presentation. – Fredrik Sep 18 '15 at 13:52
  • For completeness are there any suggestions on how to add multiple rows of values to a list from within a loop. So that its easily presented later by: "for row in final_list --> print row" – Fredrik Sep 18 '15 at 13:55
  • *sigh* Yes, there were suggestions in both answers. – Karoly Horvath Sep 18 '15 at 14:21

3 Answers3

1

I'm deleting the "old" information in list1 with *list1[:] = []

So, list1 is going to be the same object you're referencing in final_list.

final_list is simply [list1, list2]. Hence the output.

If you want a fresh object, reassign the variable:

list1 = []
Karoly Horvath
  • 94,607
  • 11
  • 117
  • 176
1

By calling list1[:] = [], you change the previous value of all list1 everywhere, even in final_list. To preserve the list1 value in final_list, you should make a copy and append the copy instead of the original list. Some explanations and nice methods for list copying can be found here.

Community
  • 1
  • 1
drali
  • 307
  • 1
  • 12
1

As many community readers pointed out, the objects within final_list are only pointers to the original objects "list1" and "list2". In the end i used the method final_list.append(list(list1)) to store the value of list1 within final_list. (As a copy) After this its safe to reset list1 and repeat the process.

Fredrik
  • 63
  • 5