functions | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 11

functions

numbers=[1,2,3,4] numbers.append([5,6,7,8]) print(len(numbers)) How does the output 5 arrives?

24th Apr 2020, 5:06 PM
PRIYANKA K
PRIYANKA K - avatar
11 Answers
+ 47
If you append [5,6,7,8] to a list that contains [1,2,3,4], the result is like this: [1,2,3,4,[5,6,7,8]]. So there are 5 elements in this nested list: 1 - 1 2 - 2 3 - 3 4 - 4 5 - [5,6,7,8] If you would have used extend() instead of append, the result would be: [1,2,3,4,5,6,7,8].
24th Apr 2020, 5:18 PM
Lothar
Lothar - avatar
+ 5
Thank you so much
25th Apr 2020, 8:03 PM
PRIYANKA K
PRIYANKA K - avatar
+ 3
The print statement return 5 because we have 5 elements in the first list and this contains another list at fifth position
9th May 2021, 10:13 AM
Excellence Kawej
Excellence Kawej - avatar
+ 1
Hi there, If you can interested to learn functional testing, you can check https://artoftesting.com/functional-testing
1st Jul 2021, 9:14 AM
rohit
0
The output is 5 because when u append the list[5,6,7,8] will get append into numbers as[1,2,3,4,[5,6,7,8]]
22nd Jul 2021, 4:54 AM
Vaishnavi Gupta
0
you added it to the list
31st Aug 2021, 5:59 AM
Haroune
Haroune - avatar
0
5 because the append is a list contains numbers
15th Oct 2021, 2:06 AM
Chi Shing Jackie Luk
Chi Shing Jackie Luk - avatar
0
just beginning here. append(5) works for adding a single value, but for a list we need extend. it's like joining a new list to exisiting one.
16th Jan 2022, 8:42 PM
Shloimy Stern
Shloimy Stern - avatar
0
Append will directly assumes it's parameter as single element before : [1, 2, 3, 4] after : [1, 2, 3, 4, [5, 6, 7, 8]]
14th Jul 2022, 5:51 AM
Konda Sandeep
Konda Sandeep - avatar
0
Yes
11th Nov 2022, 3:29 PM
Fahad Khan
Fahad Khan - avatar
0
Well, the number 5 is added in fifth position.
28th Nov 2022, 10:48 AM
Jaime
Jaime - avatar