+ 3

Output Of The Code

Can anyone tell me how the output of below code is 16: def a(): return[x+y for x in 'spam' for y in 'SPAM'] print(len(a()))

23rd Mar 2018, 12:46 PM
Aditya Rathore
Aditya Rathore - avatar
4 Answers
+ 1
for any char in "spam" add to any char in "SPAM" and add result bi-char string to a list and return it... when: [sS, sP, sA, sM, pS, pP,...] that is an list of size 4 * 4 (size of "spam" by size of "SPAM") = 16
23rd Mar 2018, 1:17 PM
KrOW
KrOW - avatar
+ 3
I changed the code a little, just run this code and see why it is 16: https://code.sololearn.com/cPKMHFg6lP2N/?ref=app
23rd Mar 2018, 4:10 PM
Paul
Paul - avatar
+ 1
That will give you the length of the return value not the length of a funcion,because a has a return value; a list of 4*4 elements of type string
23rd Mar 2018, 1:19 PM
HBhZ_C
HBhZ_C - avatar
0
Now it's clear,, thanks to you guys!
23rd Mar 2018, 1:22 PM
Aditya Rathore
Aditya Rathore - avatar