Python Challenge – Can you solve this? Today was all about deep-diving into Lists vs. Sets and I came across a common mistake that we can sometimes overlook. Let’s test your Python understanding👇 numbers = [1, 2, 3] numbers.append([4, 5]) print(len(numbers)) A) 3 B) 4 C) 5 D) Error It’s a classic interview question that tests if you truly understand how Python handles memory and lists. Day 15/30 #30DaysOfCode #DataStructures #Day15 #PythonQuiz
b) The append method will append the list as the 4th element. If the intention were to merge the list with the original list you would need to use the extend method instead.
append(_element_): the parameter is an element, which gets added to the list. extend(_list_): the parameter is a list, whose elements gets added to the list.
4, you appended a list of length 3 with one element that happens to be a list too.
Answer is 4: because in append function we are adding to the previous list so the new one i.e [4,5] it is treated as one value so the answer is 4
Answer is B, as [4,5] will be added as a single element
Answer is option b) 4 The reason is simple as append function only takes single parameters, the code given is a way to add more values in a list but it will add as a list only and a nested list ([4,5] inside [1,2,3]) is treated as a whole. Hence the length will be 4 instead of 6 or an error.
Answer is 4, because append is list method and this append one element at the end of the list , after apend list is [1,2,3,[4,5]] and length is= 4
For those who don't know, check out the difference between the append and extend methods for lists.
list=[1,2,3]-> 3 Append [4,5]-> treated as single ==1 new_list=[1,2,3[4,5]]-> 4 Answer is b)4