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 trialThomas Dimnet
Python Development Techdegree Graduate 43,629 PointsResult variable in video (value or reference)?
Hello there,
I was watching this video and I have a question related to the memory allocation subject. When we do this:
new_list = [1, 2, 3]
result = new_list[0]
What is result? Is it a copy of the reference new_list first array element (new_list[0]) or it is a copy of the value itself?
2 Answers
Dave StSomeWhere
19,870 PointsIf that example is python 3, then for your specific example that answer would be a copy/different value, since it is just a single element and creates an integer. Without seeing the vid, I would guess that the reason the variable is called "result" is to show that an evaluated expression creates a new variable. Now, if you just set another variable to the first variable the result would be having another variable name share the value value in memory (a reference).
Here's some sample python 3 code to test:
new_list = [1, 2, 3]
result = new_list[0]
list_again = new_list
print("\nTypes\n")
print('new_list is type --> ' + str(type(new_list)))
print('result is type --> ' + str(type(result)))
print('list_again is type --> ' + str(type(list_again)))
print("\ninitial values\n")
print('new_list is --> ' + str(new_list))
print('result is --> ' + str(result))
print('list_again is --> ' + str(list_again))
new_list[0] = 'abc'
list_again[2] = 'new value'
print("\nAfter change values\n")
print('new_list is --> ' + str(new_list))
print('result is --> ' + str(result))
print('list_again is --> ' + str(list_again))
# output
Types
new_list is type --> <class 'list'>
result is type --> <class 'int'>
list_again is type --> <class 'list'>
initial values
new_list is --> [1, 2, 3]
result is --> 1
list_again is --> [1, 2, 3]
After change values
new_list is --> ['abc', 2, 'new value']
result is --> 1
list_again is --> ['abc', 2, 'new value']
Thomas Dimnet
Python Development Techdegree Graduate 43,629 PointsBrendan Whiting : I think it was this one (https://teamtreehouse.com/library/accessing-a-value-in-an-array).
Dave StSomeWhere: thank you for your explanation. I did not try myself and I was looking for this kind of explanation :).
Brendan Whiting
Front End Web Development Techdegree Graduate 84,738 PointsBrendan Whiting
Front End Web Development Techdegree Graduate 84,738 PointsWhich video?