Pythonのlist
関数は、データの集まり(イテラブル)を引数に設定し、新たにリストを作成する関数です。以下にその基本的な使い方を示します。
# 空のリスト作成
sample_vac_lst = list()
print(sample_vac_lst) # []
# 文字列からリスト作成
sample_str_lst = list('Python')
print(sample_str_lst) # ['P', 'y', 't', 'h', 'o', 'n']
# 整数値0~9を要素とするリストを作成
sample_num_lst = list(range(10))
print(sample_num_lst) # [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
# リストからリストを作成
sample_copy_lst = list(sample_num_lst)
print(sample_copy_lst) # [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
# タプルからリスト作成
sample_taple_lst = list((0, 1, 2, 3, 4, 5, 6, 7, 8, 9))
print(sample_taple_lst) # [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
このように、list
関数は非常に便利で、さまざまなデータ型からリストを作成することが可能です。また、既存のリストから新たなリストを作成することも可能で、これはデータのコピーに便利です。
Pythonのlist
関数を理解し、活用することで、より効率的なコードを書くことができます。ぜひ、日々のコーディングに役立ててください。.