Python でリストをスタックとして使おうとしたときにはちょっとやりにくい。popはあるけどpushがないし、shiftもunshiftもない。なのでちょっとメモ。Ruby のスタック系メソッドとの比較で書いておく。
| Ruby | Python |
| pop | pop() |
| push(item) | append(item) |
| shift | pop(0) |
| unshift(item) | insert(0, item) |
実行例:
>>> l = ['a', 'b', 'c', 'd', 'e']
>>> l.pop()
'e'
>>> l
['a', 'b', 'c', 'd']
>>> l.append('E')
>>> l
['a', 'b', 'c', 'd', 'E']
>>> l.pop(0)
'a'
>>> l
['b', 'c', 'd', 'E']
>>> l.insert(0, 'A')
>>> l
['A', 'b', 'c', 'd', 'E']