<<

STRING / LIST//DICTIONARY

Topic STRING LIST TUPLE DICTIONARY

Type Sub Immutable Mutable Immutable Mutable Topic Accesse Index (Forward, Index (Forward, Index (Forward, Keys d by reverse) reverse) reverse)

How to Empty Str=” ” MYLIST=[ ] T=() D={} create Or Or Or MYLIST =list() T=tuple() D=dict() initialization Str=”Hello” MYLIST =*‘h’,’’,’l’,’l’,’o’+ t=(‘h’,’e’,’l’,’l’,’o’) D=,“ 1”:45,”Class 2”:55,”Class 3”:45-

Updation Not allowed MYLIST *2+=’M’ will Not allowed D[Class 3]=50 replace l with M

Addition of new Not allowed MYLIST.insert(2,”G”) will Not allowed D*“Class 4”+=45 will add new element insert G at 2 position of key and values pair the List MYLIST Or

MYLIST .append(“U”) will add U at the end of the List MYLIST MYLIST.extend (LIST2) will add the contents of List LIST2 at the end of List MYLIST Removing/deleting Not allowed Removing element by You can not allowed to Del D*“Class 2”+ will delete Element index: delete individual Class2 with its value.  MYLIST.pop() will element. You can delete D.pop() will remove last remove last element whole tuple by writing elemenpt from the  MYLIST.pop(2) will del < Tuple Name> dictionary D remove element from index poition 2 Removing element by Value: MYLIST.remove(‘h’) will remove h MYLIST.clear() will delete all elements from the list.

Counting elements X=Len(str) will X=Len(MYLIST) will X=Len(T) will count no. X=Len(D) will count no. of count no. of count no. of elements of elements present in elements present in the letters present present in the MYLIST the tuple T and store it Dictionary D and store it in in the str and and store it in the in the variable X the variable X store it in the variable X Or variable X Or X=T.count() will count X=MYLIST.count() will no. of elements present count no. of elements in the tuple T and store present in the MYLIST it in the variable X and store it in the variable X Reversing Contents THROUGH SLICING MYLIST.reverse() THROUGH SLICING : Unordered List : REV=STR[::-1] REV=T[::-1]

Sorting(Ascending/ Sorted(str) but it  MYLIST.sort() will sort Sorted(T) but it will Not possible as it is Descending) will convert the contents in convert output in the unordered list output in the ascending order form of list form of list  MYLIST.sort(reverse= True) will sort the contents in Descending order

Common Max(),min() , : max()- to find maximum value from list/tuple, min() : to find minimum for tuple and len(), count() value from list/tuple, index(‘’): will return position of element List given, count() :will count frequency of given element, len() : will length of tuple/list i.e. total no. of elements present

Common function slicing SEQ [start:end:step] * SEQ may be any string, list or tuple slice() mainly takes three parameters which have the same meaning in both constructs: for String, tuple  start - starting integer where the slicing of the object starts and List  stop - integer until which the slicing takes place. The slicing stops at index stop - 1.  step - integer value which determines the increment between consecutive number

Join Str1 + Str2 will MYLIST1 +MYLIST2 will T1 +T2 will Not allowed concatenate concatenate both lists concatenate both both strings replication Str * 3 will MYLIST * 3 will replicate T * 3 will Not allowed replicate str 3 MYLIST 3 times replicate T 3 times times UNPACKING str="abc" L=[1,2,3] T=(1,2,3) D={1:'a',2:'b',3:'c'} x,y,z=str will A,B,C=L will assign 1 to A, 2 to A,B,C=T will assign a,b,c=D will unpack(assign) assign ‘a’ to x, ‘b’ B and 3 to C from list L 1 to A, 2 to B and only keys from dictionary D to y and ‘c’ to z 3 to C from tuple T to a,b,c