LISTS:
->Lists are used to store multiple items in a single variable.
append() Adds an element at the end of the list
clear() Removes all the elements from the list
copy() Returns a copy of the list
count() Returns the number of elements with the specified value
extend() Add the elements of a list (or any iterable), to the end of the current list
index() Returns the index of the first element with the specified value
insert() Adds an element at the specified position
pop() Removes the element at the specified position
remove() Removes the item with the specified value
reverse() Reverses the order of the list
sort() Sorts the list
Opertions:
1.Create a list.
a=[1,2,3,4,5]
2.print a list
print(a) #print all elements
print(a[2]) #print only 3
print(a[2:4]) #print 3 and 4
print(a[2:]) #print 3,4,5.
print(a[:3]) #print 1,2,3
print(a[-4:-1]) #print 2,3,4
print(a[0:5:2]) #print 1,3,5[slice by 2]
print(a[-1]) #print 5
3.reverse of a list
a=[1,2,3,4,5]
print(a[::-1]) #[5,4,3,2,1]
thislist = ["banana", "Orange", "Kiwi", "cherry"]
thislist.reverse()
print(thislist) #print ['cherry', 'Kiwi', 'Orange', 'banana']
4.Length of a list
a=[1,2,3,4,5]
print(len(a)) #prints 5.
a=[1,2,3,4,5]
c=0
for i in a:
c+=1
print(c) # prints 5
5.Check item in list or not
a=[1,2,3,4,5]
if 3 in a:
print("yes")
6.Change list items
list = ["apple", "banana", "cherry"]
list[1] = "orange"
print(list) #prints apple orange cherry
list = ["apple", "banana", "cherry"]
list[1:3] = ["watermelon"]
print(list) #prints apple watermelon
7.Insert elements:
thislist = ["apple", "banana", "cherry"]
thislist[1:3] = ["watermelon"] # prints apple banana watermelon and cherry
print(thislist)
8.Append:
l = [1,2,3,4,5]
l.append(20)
print(l) #prints [1,2,3,4,5,20] [append element at last]
9.extend:
l = [1,2,3,4,5]
l1=[10,20,30]
l.exends(l1)
print(l) #print [1,2,3,4,5,10,20,30]
10.Remove element
l = [1,2,3,4,5]
l.remove(2)
print(l) #print [1,3,4,5]
11.Pop
l = [1,2,3,4,5]
l.pop()
print(l) #print [1,2,3,4]
l = [1,2,3,4,5]
l.pop(3) #3 is index not elemnet
print(l) #print [1,2,3,5]
12.Clear
l = [1,2,3,4,5]
l.clear()
print(l) #prints []
13.loops in list
l=[1,2,3,4,5]
for i in l:
print(i,end=" ") #print 1 2 3 4 5
l=[1,2,3,4,5]
[print(i,end=" ") for i in l] #print 1 2 3 4 5
l=[1,2,3,4,5]
i=0
while(i<len(l)):
print(l[i])
i+=1 #print 1 2 3 4 5
14.List comprehension
fruits = ["apple", "banana", "cherry", "kiwi", "mango"]
newlist = []
for x in fruits:
if "a" in x:
newlist.append(x)
print(newlist) #print ['apple', 'banana', 'mango']
15.sort list
t= [100, 50, 65, 82, 23]
t.sort()
print(t) #print [23, 50, 65, 82, 100]
t= [100, 50, 65, 82, 23]
t.sort(reverse = True)
print(t) #print [100, 82, 65, 50, 23]
16.Copy list
thislist = ["apple", "banana", "cherry"]
mylist = thislist.copy()
print(mylist) #print ["apple", "banana", "cherry"]
thislist = ["apple", "banana", "cherry"]
mylist = list(thislist)
print(mylist) #print ["apple", "banana", "cherry"]
17.List join
list1 = ["a", "b", "c"]
list2 = [1, 2, 3]
list3 = list1 + list2
print(list3)
No comments:
Post a Comment