0

Could anyone here tell me how to properly append series of missing values onto a python list?

for example,

 > ls=[1,2,3]
 > ls += []*2
 > ls
  [1,2,3]

but this is not the outcome I want. I want:

   [1,2,3, , ]

where the blanks denotes for the missing values.

(note: also what I DON'T want is:

   > ls
   [1,2,3,'','']

)

Thanks,

chico0913
  • 487
  • 1
  • 10
  • 17

2 Answers2

0

use list.extend, it will Extend list by appending elements from the iterable.

ls=[1,2,3]
ls.extend(['']*2)

output

 [1,2,3,'', '']

whereas list.append Append object to the end of the list.

ie [1,2,3].extend([4]) - > [1,2,3,4]

`[1,2,3].extend([[4]])` -> `[1,2,3,[4]]`
sahasrara62
  • 7,680
  • 2
  • 24
  • 37
0

You can do like this.

>>> ls=[1,2,3]
>>> ls += ['']*2
[1, 2, 3, '', '']
Florian Bernard
  • 2,473
  • 1
  • 8
  • 22