2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Python リスト(list)について

Posted at

リスト(list)とは

タプルと違い,作成後に要素の追加や削除が出来ます.いつでも要素の変更が出来るため,非常に汎用性が高いです.

ソースコード

list.py
test_list_1 = ['python', 'abc', 'efg', 'python', '1000', 'abc', 'abc']
print(test_list_1)

print('-----------------------------')

for i in test_list_1:
    print(i)

test_list_2 = []
print(test_list_2)

print('-------------------------------')
# 要素の追加
test_list_2.append('python')
test_list_2.append('web')
test_list_2.append('aaaaa')
test_list_2.append('zzzzz')
print(test_list_2)

print('-------------------------------')
# インデックスを指定して追加
test_list_1.insert(1,',')
test_list_1.insert(3, '-')
print(test_list_1)

print('----------------------------------')
# 要素の削除_1
test_list_1.remove('abc')
print(test_list_1)

print('----------------------------------')
# 要素の削除_2
print(test_list_2.pop(3))
print(test_list_2)

print(test_list_2.pop())
print(test_list_2)

print('----------------------------------')
# 要素のインデックスを取得
print(test_list_1.index('efg'))

print('----------------------------------')
# リスト内での要素数の取得
print(test_list_1.count('python'))
['python', 'abc', 'efg', 'python', '1000', 'abc', 'abc']
-----------------------------
python
abc
efg
python
1000
abc
abc
[]
-------------------------------
['python', 'web', 'aaaaa', 'zzzzz']
-------------------------------
['python', ',', 'abc', '-', 'efg', 'python', '1000', 'abc', 'abc']
----------------------------------
['python', ',', '-', 'efg', 'python', '1000', 'abc', 'abc']
----------------------------------
zzzzz
['python', 'web', 'aaaaa']
aaaaa
['python', 'web']
----------------------------------
3
----------------------------------
2

参考にさせていただいたもの

リスト:Python-izm
https://www.python-izm.com/basic/list/

2
1
0

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
2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?