Python Forum
Newbie query regarding lists
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Newbie query regarding lists
#1
I am just working through some intros to Python. Basically I read about a concept and then have a play around to make sure I understand it. Right now, I'm on lists.

I wrote the following program to test a few commands:
test_list = [1,2,3,4]
print (test_list[0])
print (test_list[2])
print (test_list[-1])
sub_test_list = test_list[1:3]
print (sub_test_list)
naturals = [0,1,2,3,4,5,6,7,8,9,10,11,12]
evens = naturals[2::2]
print(evens)
odds = naturals[1::2]
print(odds)
primes = odds
primes[4:6]=[11,13]
print(odds) # OP originally had this bolded
print(primes)
naturals.append(13)
print(naturals)
The bit that confused me is the command 'print(odds)' towards the bottom in bold. I was still expecting odds to be [1,3,5,7,9,11] as the change was applied to the list 'primes'. However, both 'odds' and 'primes' were printed as [1,3,5,7,11,13].

Please could someone explain to me why this is the case?

Thanks

Stephen
Reply
#2
On line 12, the assignment reuses the same object. It's not a copy. So when one is modified, so is the other. You can make a "shallow" copy by replacing line 12 with
primes = odds[:]
This works because with [:] the variable "primes" no longer refers to the same object.
Reply
#3
Thank you very much
Reply
#4
Can I follow up with one additional question? I appreciate how to fix my initial query, but am not sure why the assignment in the original code reuses the object whereas in the code below it appears not to.

x=8
print('x=',x)
z=x
print('x=',x)
print('z=',z)
z=z+2
print('x=',x)
print('z=',z)
i.e. when z is modified x remain unmodified.

I assume it is that
variable2 = variable1
assigns variable2 independent to variable1 going forward (just of identical value at this stage) whereas
datatype2 = datatype1
assigns datatype2 and datatype1 to be identical going forward such that changes to one are automatically applied to the other.

However, I'd like to be 100% sure before moving on.
Reply
#5
Great question. Basically, lists are mutable, meaning can be edited. Numbers cannot change, though the variable that points to a number can be changed to point to a new number, which is what happens in your code. For this issue you've run into with lists, you won't run into with immutable types like numbers, tuples and frozensets.
Reply
#6
Thanks again. Want to spend time getting the basics right before moving on (and only installed Python on Thursday!)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Split dict of lists into smaller dicts of lists. pcs3rd 3 2,367 Sep-19-2020, 09:12 AM
Last Post: ibreeden
  sort lists of lists with multiple criteria: similar values need to be treated equal stillsen 2 3,261 Mar-20-2019, 08:01 PM
Last Post: stillsen
  Newbie lists problem LedDiode 5 3,653 Dec-16-2018, 08:33 PM
Last Post: LedDiode
  Newbie seeking help with DNS query DaytonJones 0 2,216 Sep-21-2018, 06:29 PM
Last Post: DaytonJones

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020