0

What is the correct syntax for creating a variable that is bound to a list?

a) my_list = [2, ā€˜appleā€™, 3,5] b) my_list = [2, ā€˜appleā€™, 3,5].to_list c) my_list = to_list(2, ā€˜appleā€™ 3,5) d) my_list = (3, ā€˜appleā€™, 3,5) I think it is a but I am not so sure about, my search is also not giving me more certainty. Please kindly advise

24th Nov 2019, 11:27 AM
THEGreatGatsby
THEGreatGatsby - avatar
5 Answers
+ 4
a is right. If you have another container, for example a tuple (let's say its name is 'stuff'), you can create a list with the same content: my_list = list(stuff)
24th Nov 2019, 11:44 AM
HonFu
HonFu - avatar
+ 4
[1] I suppose you're right. The thing is, in Python you never put something into memory directly, but - that's the way I put it - you put name stickers onto objects (that are created secretly in the background). Every name is a reference. In case of a list, when you write... a = [1, 2, 3] ... a fresh list is created, and then the name a is given to refer to it. Python keeps track of this object and all its 'name stickers'. Later, you append the list to another list like... onemorelist.append(a) Now Python stores another reference ('sticker') to your list. Now if you write... del a ... not the list itself is deleted, but only the name 'a' is erased from the object's sticker list. Then Python checks, if there are still stickers left. There's one, the reference in that other list, so the object is kept around. Now if you delete the list from the other list, the 'reference count' sinks to zero. NOW (sometimes a bit later) Python actually deletes the list without you knowing.
24th Nov 2019, 11:52 AM
HonFu
HonFu - avatar
+ 3
[2] Now writers who write stuff like 'binds a name to' probably do it with the generally good idea of not giving the student wrong information. While in a language like C you can quite well describe a variable name as a box where you put stuff in, this description is not exactly true in Python, where you as a person never get to touch computer memory directly - it's all done for you, and you only get a 'reference'. Problem is that a beginner can hardly understand this, and the aforementioned way of putting it just leads to confusion. Which may be seen as one good argument to learn C, before you learn Python, so that you first get an idea of what MIGHT be kind of happening in the background, when you write Python code. But well, the other direction also works. ;-)
24th Nov 2019, 12:17 PM
HonFu
HonFu - avatar
+ 2
so basically to say that we are binding a variable to a list is anytime we create a list for instance list1 = [ 1,5,8] or list2 = [ā€˜appleā€™, ā€˜toadā€™, ā€˜hoaxā€™] just fancy name binding to a list
24th Nov 2019, 11:47 AM
THEGreatGatsby
THEGreatGatsby - avatar
0
I'm new to programming. When I saw this question, it really threw me for a loop. I know what a variable is and I know what a list is... but, I did not understand the question. Thank you to others above for patiently explaining. Question is just asking to create a list and assign to a variable...? This is how I interpret it. =/
21st Apr 2023, 2:13 AM
M
M - avatar