+ 2

Why is i = [1,2,3,4]*(-1) turns out to be [ ]?

2nd Sep 2018, 12:09 PM
龍司南
龍司南 - avatar
3 Réponses
+ 10
list * n will return a new list with the items of "list" repeated n times. So i*2 would be [1, 2, 3, 4, 1, 2, 3, 4]. list * (anything < 1) will return an empty list because the elements of the list are returned / repeated 0 times.
2nd Sep 2018, 12:25 PM
Anna
Anna - avatar
+ 3
you probably want: [-n for n in i]
2nd Sep 2018, 12:45 PM
davy hermans
davy hermans - avatar
+ 1
Multiplying a list with a negative number returns an empty list. What else would you like to get?
2nd Sep 2018, 12:30 PM
michal