+ 1

Appending in Python. Not working

cars = ['audi', 'vw', 'subaru'] cars.append('fiat') len(cars) . #nothing happens here Nothing happens. (No counting) Why??

20th Jun 2018, 12:02 PM
Arthur P
Arthur P - avatar
7 Answers
+ 3
why would you expect something to happen? you don't print out the length at all
20th Jun 2018, 12:06 PM
hinanawi
hinanawi - avatar
+ 2
Max Rubs because there, code you type is forwarded into the output immediately, so it just prints it no matter what, and as append returns void, nothing is printed previously (iirc)
20th Jun 2018, 12:28 PM
hinanawi
hinanawi - avatar
+ 1
but if you add: print(len(cars)) it should output 4 the code itself is fine. but as hinanawi said, len(object) does not produce anything.
20th Jun 2018, 12:18 PM
Marco Polidori
Marco Polidori - avatar
+ 1
TY Marco! It works! Don't you know why my way works in IDLE?
20th Jun 2018, 12:27 PM
Arthur P
Arthur P - avatar
0
I meant it doesn't count anything((
20th Jun 2018, 12:10 PM
Arthur P
Arthur P - avatar
0
Max Rubs len() returns an integer, so you need to store the value somewhere for anything to happen
20th Jun 2018, 12:11 PM
hinanawi
hinanawi - avatar
0
Strange, it works in the python shell >>> cars = ['audi', 'vw', 'subaru'] >>> cars.append('fiat') >>> len(cars) 4
20th Jun 2018, 12:24 PM
Arthur P
Arthur P - avatar