본문 바로가기
Data Science

[Python] List

by kimpackung 2021. 8. 8.
728x90

List

1) Single variable containing multiple items

2) Using square brackets

3) Items are ordered, changeable, and can be duplicated.

4) Items are indexed

 

 

<Code>

1) create a list

#make list
list=[]

2) add items

#add items to list by using append
list.append(1)

#a item in the another list also can be added
a=[1,2,3]
list.append(a[1])

#list itself can be a item of another list
list.append(a)
print(list)

#adding elements of list one by one to another list
#by using 'for'

list2=[]
for i in range(len(a)):
  list2.append(a[i])

3) Remove elements

#delete elements

#remove element '1'
list3=[1,2,3]
list3.remove(1)
print(list3)

#remove 1th element of the list
list4=[1,2,3]
list4.remove(list4[1])
print(list4)

#remove last element of the list
list5=[1,2,3]
list5.pop()
print(list5)

#remove every elements in the list
list6=[1,2,3]
list6.clear()
print(list6)

 

댓글