Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python

equality question with using splices and lists vs variables (numbers)

when assigning a variable ("b") equal to another variable ("a"), then change the value "b" to equal something else, "a" does not change.

example:

a = 1
b = a
b = 2

"a" stays equal to 1 and "b" is equal to 2, not 1.

However when using lists, the same is not true unless it uses a splice "[:]":

example:

 favorite_things = ['raindrops on roses', 'whiskers on kittens', 'bright copper kettles',
                   'warm woolen mittens', 'bright paper packages tied up with string',
                   'cream colored ponies', 'crisp apple strudels']

sorted_things = favorite_things
sorted_things.sort()

Now, favorite_things and sorted_things =

['bright copper kettles', 'bright paper packages tied up with string', 'cream colored ponies', 'crisp apple strudels', 'raindrops on roses', 'warm woolen mittens', 'whiskers on kittens']

unless written as sorted_things = favorite_things[:]

I would think assigning one list to a new name would not change the original list when modifying the new name.

why?

[MOD: applied formatting -cf]

2 Answers

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,468 Points

Great question! Python is a "object-reference" meaning that each object is stored in memory and "variables" and object references simply point to the object's location in memory. The id() function will return the memory location of any object. If two objects have the same id, they are the same object.

# running each statement then checking the object id values
>>> a = 1
>>> id(a)
1690247888
>>> id(1)
1690247888
>>> b = a
>>> id(b)
1690247888
# a and b have same id. They reference the *same* object
>>> b is a
True
>>> b = 2
>>> id(b)
1690247920
>>> id(2)
1690247920
# notice there are only two objects: the integer 1 and the integer 2
>>> favorite_things = ['raindrops on roses', 'whiskers on kittens', 'bright copper kettles',
...                    'warm woolen mittens', 'bright paper packages tied up with string',
...                    'cream colored ponies', 'crisp apple strudels']
>>> id(favorite_things)
19460360
>>> sorted_things = favorite_things
>>> id(sorted_things)
19460360
>>> sorted_things.sort()
>>> id(sorted_things)
19460360
# Both sorted_things  and favorite_things references the same object
# they are still equal:
>>> sorted_things == favorite_things
True
# and they are the *same* object
>>> sorted_things is favorite_things
True
# a slice, creates a new object and leaves the original is unchanged
>>> id(favorite_things)
19460360
>>> sorted_things = favorite_things[:]
>>> id(sorted_things)
19460424
# sorted_things has a new id since it is a new object

# they are still equal:
>>> sorted_things == favorite_things
True
# but now they are *not* the same object
>>> sorted_things is favorite_things
False

By the way, the is comparator basically is comparing id()` of both objects to see if they are the same object.

Post back if you have more questions. Good luck!!

Thank you!