高校物理160203 views
高校国語788336 views
高校化学2924392 views
雑学1473612 views
高校日本史190571 views
小学社会310485 views
英語613639 views
小学算数1200427 views
りんご209490 views
高校生物551766 views
Help
Tools
NewsSpreadsheetCalendarSlidesTier ListPen ToolIllustrationCrayonPixel ArtASCII ArtPerspectiveEndless StairsGraphMind MapER DiagramFamily TreeMemeCurved TextImage EditorMosaicRetro FilterPencil SketchOCR/HighlighterMakeup EditorFaviconVideo TrimmerScrolling VideoVideo TitleColor PickerColor ExtractorBonfireFireworksWater RippleWater SplashBreaking GlassWood GrainMarble TextureCSS ButtonIcon MakerBar ChartGrouped Bar ChartStacked Bar ChartPie ChartLine ChartArea ChartStacked Area ChartScatter Plot3D Bar Chart3D Pie ChartBar Chart RaceBubble ChartPopulation PyramidPictogramEarningsCandlestick ChartInvestment RiskMortgage SimulatorCalculatorMatrix CalculatorFunction GraphPolynomial ExpansionVenn DiagramField VisualizerRubik's Cube Group TheoryTraveling SalesmanVoronoi and DelaunayFractalColumn ArithmeticDraw Math FiguresArithmetic AnimationArithmetic Word ProblemsCounting with Tree DiagramsCube NetsCross SectionsMotion PathMechanicsWavesLight and LensesThermodynamicsHow Semiconductors WorkMolecular StructuresAtomic OrbitalsElectrochemical CellsChemical EquilibriumCrystal LatticesBuffer pHComplex IonsDNA Double HelixCell DivisionMembrane ChannelsNerve ImpulseRock ClassificationWeatherConstellationsSolar and Lunar Eclipses3D ModelingFloor PlanSeismic StructuresIntersection TurnMaglevCooking AnimationOrigamiLive Viewer CountGeoJSON MapRailway MapPopulation MapCrime MapLand Price MapSchool MapShrine and Castle MapHouse of Representatives MapWord MapSolitaireReversiHakoiri MusumeChessHamburgerRippleSlide Puzzle MakerNeon PinballNovel MakerJapanese Typing PracticePiano Score EditorMusic Theory

English

Pythonでリストから要素を削除する(del)

Pythonではリストから特定の要素を削除できます。これはpopメソッドと同じです。

プログラム

a = [3, 7, 11, 15, 19]
del a[1]
print(a)

出力

a[1]は7を表しますが、それをdelで削除することで、aは[3, 11, 15, 19]というリストになります。これは次のプログラムとまったく同じです。

プログラム

a = [3, 7, 11, 15, 19]
a.pop(1)
print(a)

出力

インデックスが要素数を越えるとき

5つしか要素がない数列の10番目を削除しようとすると

プログラム

a = [3, 7, 11, 15, 19]
del a[10]
print(a)

出力

Traceback (most recent call last):
File “list_del.py”, line 2, in
del a[10]
IndexError: list assignment index out of range

インデックスエラーが出ます。

インデックスがマイナスのとき

プログラム

a = [3, 7, 11, 15, 19]
del a[-1]
print(a)

出力

リストではマイナスのインデックスは後ろからの順番を意味しますが、delも同じように認識します。

リストのリストを削除する

リストのリストもdelでリストを削除できます。

プログラム

b = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
print(b)
del b[1]
print(b)

出力

[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
[[1, 2, 3], [7, 8, 9]]

b[1]の[4, 5, 6]が削除されています。

目次(リスト)

リストの基本
リストの長さ
整数のリスト(range)
インデックスから値を取得
マイナスのインデックス
値の存在チェック
値の重複カウント
値の追加
リストの追加
リストの足し算
リストのかけ算
リストの挿入
並び替え(逆順)
並び替え(昇順)
並び替え(降順)
値の削除(remove)
値の削除(pop)
値の削除(del)
リストの総和

Python

環境設定
文字列
数値
リスト
辞書
集合
ifとelse
forとwhile
関数
クラス
ファイル
システム

Pythonでリストから要素を削除するにはdelを使います。これはリストのリストでも使えます。