-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path13_List.py
More file actions
61 lines (47 loc) · 1.6 KB
/
Copy path13_List.py
File metadata and controls
61 lines (47 loc) · 1.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#list-Access-change-Add-Remove
mylist=["Apple", "Banana", "Orange"]
print(mylist)
print(len(mylist)) #length of a list
print(type(mylist)) #Getting list type. all list in python is a list type data
#cunstructor to make list
listCons = list(("apple","Cherry",True, 100))
print(listCons)
#Access item
print(listCons[1]) # first index is 0.
print(listCons[-1]) # Negative indexing means from last
print(listCons[2:4]) #Getting ranges of list
print(listCons[:3]) #By leaving out the start value, the range will start at the first item
print(listCons[0:]) # By leaving out the end value, the range will go on to the end of the list
print(mylist[-1:-3]) #Start searching from negative index
list2 = ["Apple", "Banna", "Grape"]
if "Apple" in list2:
print("Yes,It's here")
#Change a item value
change = ["Apple", "Orange", "Banana", "Jackfruit", "Mangoes", "Tomatos", "Guava"]
change[1:3] = ["Cherry", "Watermelon"]
print(change)
#inserting new item without changing other item in a fixed index
change.insert(7, "fish")
print(change)
#insertinh new item at the end
change.append("Last item")
print(change)
#To append elements from another list to the current list
change.extend(list2)
print(change)
#Remove first item
change2 = ["Apple", "Orange", "Banana"]
change2.remove("Apple")
print(change2)
#Remove specified item with index
change.pop(1) # if dont specified then it will remove last item
#using del keyward
del change2[0]
print(change)
change3 = ["orange","apple", "Banana"]
del change3
#will delete full list as I didn't mentioned index
#Clearing list
clear = ["Apple", "Orange", "Banana"]
clear.clear()
print(clear)