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
  • 577
  • 4
  • 10
  • 22

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
  • 10,069
  • 3
  • 29
  • 44
0

You can do like this.

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