List - Ipynb - Colab
List - Ipynb - Colab
List - Ipynb - Colab
list
list create [] (changeble,allow duplicate value, ordered) access (index,slice) operation (inser
tuple create access add delete operation
set create access add delete operation
dictionary create access add delete operation
List
Sequence types such as lists, tuples, and strings are always ordered
list contains different types of data items stored in the list seperated with commas(,) enclosed within sqaure bracket[ ]
[1 ,3 ,4 ,1 ,6,"a"]
[1, 3, 4, 1, 6, 'a']
type( [1 ,3 ,4 ,1 ,6] )
list
#set dont allow duplicate value, consider length one of same items
fruits={"apple","banana","cherry","cherry","apple"}
print(fruits)
print(len(fruits))
{'banana', 'apple', 'cherry'}
3
List items
List items are inedexed, the first item has index[0], the second item has index[1] etc.
Access items
List items are indexed, and you can access them by referring to the index numbers.
# 0 1 2 3 4
fruit_list=["Apple","Mango","Orange","Grapes","Bananas"]#list
print("List:",fruit_list[3])
List: Grapes
Negative Indexing
# -5 -4 -3 -2 -1
fruit_list=["Apple","Mango","Orange","Grapes","Bananas"]#list
print("List:",fruit_list[-4])
List: Mango
slice
# -5 -4 -3 -2 -1
fruit_list=["Apple","Mango","Orange","Grapes","Bananas"]#list
print("List:",fruit_list[:-2]) # start 0 end -2 --- 0 to -3
List: ['Apple', 'Mango', 'Orange']
fruit_list=["Apple","Mango","Orange","Grapes","Bananas"]#list
if "Apple" in fruit_list:
print("Exists!")
print("hello")
#{} indentation
Exists!
hello
fruit_list=["Apple","Mango","Orange","Grapes","Bananas","Watermelon"]#list
if "kiwi" in fruit_list:
print("Exists!")
print("hello")
hello
#index0 1 2 3 4 5
# 1 2 3 4 5 6
list=[10,20,30, 'ten','twenty','thirty']
print("complete list:",list)
print("4th element from the list list[3]=", list[3])
print("element from 1 to 3 list[0:3]=", list[0:3])
print("element from 4 to 5 list[3:5]=", list[3:5])
print("element from 6 to end list[4:]=", list[4:])
complete list: [10, 20, 30, 'ten', 'twenty', 'thirty']
4th element from the list list[3]= ten
element from 1 to 3 list[0:3]= [10, 20, 30]
element from 4 to 5 list[3:5]= ['ten', 'twenty']
element from 6 to end list[4:]= ['twenty', 'thirty']
fruits=["apple","banana","cherry","pineapple","guava"]
if "guava" in fruits:
print("guava is present")
guava is present
# 0 1 2 3 4
fruits=["apple","banana","cherry","pineapple","guava"]
fruits[1]="watermelon"
print(fruits)
['apple', 'watermelon', 'cherry', 'pineapple', 'guava']
fruits=["apple","banana","cherry","pineapple","guava"]
fruits[1]="watermelon"
fruits[2]="banana"
print(fruits)
['apple', 'watermelon', 'banana', 'pineapple', 'guava']
To change the value of item within a specific range , define a list with the new values and refer to the range of index numbers where you want to
insert new values.
If you insert more item then you replace , the new items will be inserted where you specified, and the remaining items will move accordingly.
# 0 1 2 3 4
fruits=["apple","banana","cherry","pineapple","guava"]
newfruits=["watermelon","muskmelon"]
print(fruits[1:3])
fruits[1:3]=newfruits
print(fruits[1:3])
print(fruits)
['banana', 'cherry']
['watermelon', 'muskmelon']
['apple', 'watermelon', 'muskmelon', 'pineapple', 'guava']
# 0 1 2 3 4
fruits=["apple","banana","cherry","pineapple","guava"]
newfruits=["watermelon","kiwi","peach"]
fruits[1:3]=newfruits
print(fruits)
['apple', 'watermelon', 'kiwi', 'peach', 'pineapple', 'guava']
# 0 1 2 3 4
fruits=["apple","banana","cherry","pineapple","guava"]
newfruits=["watermelon","kiwi","peach"]
fruits[1]=newfruits
print(fruits)
['apple', ['watermelon', 'kiwi', 'peach'], 'cherry', 'pineapple', 'guava']
fruits=["apple","banana","cherry","pineapple","guava"]
newfruits=["watermelon","kiwi","peach"]
fruits[2]=newfruits
print(fruits)
['apple', 'banana', ['watermelon', 'kiwi', 'peach'], 'pineapple', 'guava']
insert operation
# -5 -4 -3 -2 -1
fruits=["apple","banana","cherry","pineapple","guava"]
fruits.insert(-1,"watermelon")
print(fruits)
['apple', 'banana', 'cherry', 'pineapple', 'watermelon', 'guava']
# 0 1 2 3 4
# -5 -4 -3 -2 -1
fruits=["apple","banana","cherry","pineapple","guava"]
print(fruits)
print(len(fruits))
fruits.insert(-3,"watermelon")
print(fruits)
print(len(fruits))
['apple', 'banana', 'cherry', 'pineapple', 'guava']
5
['apple', 'banana', 'watermelon', 'cherry', 'pineapple', 'guava']
6
fruits=["apple","banana","apple","cherry","pineapple","guava"]
print(len(fruits))
6
Insert Items
To insert a new list item, without replacing any of the existing value, we can use the insert() method.
fruits=["apple","banana","cherry","pineapple","guava"]
fruits.insert(5,"watermelon")
print(fruits)
['apple', 'banana', 'cherry', 'pineapple', 'guava', 'watermelon']
fruits=["apple","banana","cherry","pineapple","guava"]
fruits.insert(4,"watermelon")
print(fruits)
['apple', 'banana', 'cherry', 'pineapple', 'watermelon', 'guava']
fruits=["apple","banana","cherry","pineapple","guava"]
fruits.insert(-2,"watermelon")
print(fruits)
['apple', 'banana', 'cherry', 'watermelon', 'pineapple', 'guava']
fruits=["apple","banana","cherry","pineapple","guava"]
fruits.insert(-1,"watermelon")
print(fruits)
['apple', 'banana', 'cherry', 'pineapple', 'watermelon', 'guava']
Append Item
Extend List
To append elements from another list to the current list use the extend() method.
fruits=["apple","banana","cherry","pineapple","guava"]
newfruits=["watermelon","peaches"]
fruits.extend(newfruits)
print(fruits)
['apple', 'banana', 'cherry', 'pineapple', 'guava', 'watermelon', 'peaches']
fruits=["apple","banana","cherry","pineapple","guava"]
fruits.remove("banana")
print(fruits)
['apple', 'cherry', 'pineapple', 'guava']
if you do not specify the index, the pop() method removes the last item.
fruits=["apple","banana","cherry","pineapple","guava"]
fruits.pop(1)
print(fruits)
['apple', 'cherry', 'pineapple', 'guava']
fruits=["apple","banana","cherry","pineapple","guava"]
fruits.pop()
print(fruits)
['apple', 'banana', 'cherry', 'pineapple']
The del keyword can also delete the list items compeletely and in the output it gives a empty list
fruits=["apple","banana","cherry","pineapple","guava"]
del fruits[0]
print(fruits)
['banana', 'cherry', 'pineapple', 'guava']
# The del keyword can also delete the list items compeletely and in the output it gives a empty
fruits=["apple","banana","cherry","pineapple","guava"]
del fruits[:]
print(fruits)
[]
The clear method empties the list. The list still remains, but it has no content.
fruits=["apple","banana","cherry","pineapple","guava"]
fruits.clear()
print(fruits)
[]