I am trying to initiliase a list with the first 5 elements empty and then append to the list thereafter. The purpose is that when i write this list to a csv file, the output will be as such: ,,,,,,a,b,c here is my code:
l = list()
l[:5]=""
l.append('a')
When i print out this list, it contains only the element 'a'. How can I initiliase the list such the first 5 elements are empty and when I print it out it will show something like [, , , , , 'a']
thanks
For immutable types like string you can do -
lst = [""] * 5
Though doing this for mutable elements (like lists or so) can cause issues as all the five elements would be pointing to the same object , so mutating one of them would cause changes reflected in the other elements (as they are the same object) .
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With