Bumped into a programming issue that puzzles me a bit. Im parsing data and:
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)]
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With