LoginSignup
6
13

More than 5 years have passed since last update.

Python3 リスト・タプル・辞書・集合の基本的な操作一覧

Last updated at Posted at 2017-03-24

概要

Pythonのリスト・タプル・辞書・集合の基本的な操作について忘れやすいのでメモ。Python 3.x系での挙動。

is mutable?

リスト タプル 辞書 集合
Yes No Yes Yes

基本操作早見表

操作 リスト タプル 辞書 集合
空コレクションの生成 list()
[ ]
tuple()
( )
dict()
{ }
set()
リテラル [0, 1] (0, 1) {'A': 0, 'B': 1} {0, 1}
複製 (浅いコピー) list(a)
a.copy()
a[:]
tuple(a)
代入
dict(a)
a.copy()
set(a)
a.copy()
追加 a.append(val) - a[key] = val a.add(val)
結合 (in-place) a.extend(b)
a += b
- a.update(b) a.update(b)
a |= b
結合 (not-in-place) a + b a + b - a | b
a.union(b)
結合 (starred expression1) [*a, *b] (*a, *b)
*a, *b
{**a, **b} {*a, *b}
挿入 (単一要素) a.insert(idx, val)
a[idx:idx] = val,
- - -
挿入 (複数要素) a[idx:idx] = b - - -
積集合 - - - a & b
a.intersection(b)
差分 - - - a - b
a.difference(b)
削除 (値指定) a.remove(val) - - a.remove(val)
削除 (キー指定) del a[idx]
del a[start:stop:step]
- del a[key] -
全削除 a.clear()
del a[:]
- a.clear() a.clear()
要素数 len(a)
a.count()
len(a) len(a) len(a)


  1. Python 3.5から使用可能。 

6
13
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
13