+ 2

List clear()

What is clear() method any one explain me in comment

11th Jun 2020, 10:30 AM
Muhammad Nouman
Muhammad Nouman - avatar
6 ответов
+ 7
[edited: ***] The clear() method for lists has a behaviour that has to be considered. I have done this demo in the console, but same thing happens in a regular python file that is executed: >>> res = [24,2] # create res with some numbers >>> buf = [1,2,3] # create buf with also some numbers >>> res.append(buf) # now append buf to res >>> res # res is now: [24, 2, [1, 2, 3]] >>> buf.clear() # now clear buf >>> res # this is what res looks now: [24, 2, []] *** i remember that HonFu has done a great tutorial that explaines why things like these happens.
11th Jun 2020, 11:08 AM
Lothar
Lothar - avatar
+ 4
clear() deletes all entries. Please begin to google such general questions. Finding good pages and learn to read the docu of a method ist very important and one should learn it very soon. For beginner I recommend w3schools. https://www.w3schools.com/JUMP_LINK__&&__python__&&__JUMP_LINK/ref_list_clear.asp.
11th Jun 2020, 10:37 AM
Oma Falk
Oma Falk - avatar
+ 4
The mentioned tutorial was this one. Yeah, aforementioned case clearly a reference issue. https://code.sololearn.com/c89ejW97QsTN/?ref=app
11th Jun 2020, 11:45 AM
HonFu
HonFu - avatar
+ 3
Lothar what is surprising on that?
11th Jun 2020, 11:22 AM
Oma Falk
Oma Falk - avatar