Pb-U7.3 List
Pb-U7.3 List
LIST
Introduction :
o LIST is a collection of items and each item has its own index value.
o List is a standard data type of Python. It is a sequence which can store values of any kind.
o List is represented by square brackets “[ ] “
o A List is a mutable data type which means any value from the list can be changed.
o List is a sequence like a string and a tuple except that list is mutable whereas string and tuple
are immutable.
o In Python List is a sequence of items and each items can be individually access using index.
From beginning the first item in List is at index 0 and last will be at len-1. From backward
direction last item will be at index -1 and first item will be at –len.
Forward indexing
0 1 2 3 4 5 6 7 8 9 Positive Index
70 10
10 20 30 40 50 60 80 90 Values
0
-10 -9 -8 -7 -6 -5 -4 -3 -2 -1 Negative Index
Backward indexing
Creating & Initializing List:
Creating a list
Lists are enclosed in square brackets [ ] and each item is separated by a comma.
Syntax: ListName = [ ]
o Creating list from existing sequences (i.e. Using list() function…
Syntax: ListName = list(sequence)
Code: Output:
>>>Mylist = list(‘Hello’) [‘H’,’e’,’l’,’l’,’o’]
>>>Mylist
Code: Output:
>>>Mylist = (‘H’,’e’,’l’,’l’,’o’) [‘H’,’e’,’l’,’l’,’o’]
>>>Mylist = list(Mylist)
>>>Mylist
Initializing a list
Passing value in list while declaring list is initializing of a list.
Syntax: ListName = [ value1, value2,…..]
0 1 2 3 4 5 6 7 8 9
10 20 30 40 50 60 70 80 90 100
-10 -9 -8 -7 -6 -5 -4 -3 -2 -1
Code: Output:
>>> n=[ 10,20,30,40,50,60,70,80,90,100]
>>>n[1] 20
>>>n[5] 60
>>>n[-3] 80
>>>n[15] Index Error
>>>n[3:8] [40,50,60,70]
List operators
o There are many operations that can be performed with List which makes it one of the most
used data types in Python.
o Two basic operators + and * are allowed
+ is used for concatenation (joining)
* Is used for replication (repetition)
List Slicing
o As we know slice means “part of”, so slicing is a process of extracting part of list.
o OR Slicing is a part of a list containing some continuous character from the list.
o The list are sliced using a range of indices (List characters have their unique index position
from 0 to length-1 and -1 to –length(backward)).
o Syntax: seq = list [ start : stop ] OR seq = list [ start : stop : step ]
Where start & stop are integer, and start is staring index value, stop is ending index
value & step is stepping index.
o Example1: Lst1 = [ 10 , 20 , 30 , 40 , 50 , 60 , 70 , 80 , 90 ]
Lst1[ : ] / Lst1[ : :] / Lst1 [ 10, 20, 30, 40, 50, 60, 70, 80, 90 ]
Lst1[ 2 : 5 ] [ 30, 40, 50 ]
Lst1[ 3 : -3 ] [ 40, 50, 60 ]
Lst1[ 0 : 10 : 2 ] [ 10, 30, 50, 70, 90 ]
Lst1[ : : 3 ] [ 10, 40, 70 ]
Lst1[ : : -1 ] [ 90, 80, 70, 60, 50, 40, 30, 20, 10 ]
Lst1[ : : -2 ] [ 90, 70, 50, 30, 10 ]
Lst1[ -4 : -1 ] [ 60, 70, 80 ]
Lst1[ : : 3 ] [ 10, 40, 70 ]
Lst1[ 4 ] [ 50 ]
Lst1[ 15 ] IndexError: list index out of range
print(L) iterable
index(substring) :
o This function returns index position of substring.
o This function is used to get the index of first matched item from the list. It returns index value
of item to search
o Syntax : <List_Object>.index(substring) :
o Example:
Code: Output:
Lst=[10,20,30,40,50,60,70,80,90,100] 4
print(Lst. index (50))
Code: Output:
Lst=[10,20,30,40,50,60,70,80,90,100] Value error: Substring not
print(Lst. index (200)) found.
len(List) :
o This function returns the number of characters in any list including spaces.
o Syntax : len( <List_Object> )
o Example:
Code: Output:
Lst=[10,20,30,40,50,60,70,80,90,100] 10
print(len(Lst))
Code: Output:
Lst=[‘I’,’N’,’D’,’I’,’A’] 5
print(len(Lst))
o Example:
Code: Output:
Lst = [ 10, 20, 30, 40]
Lst.append(50)
print(Lst) [10, 20, 30, 40,50]
Lst.append([60, 70])
print(Lst) [10, 20, 30, 40, 50, [60, 70]]
Lst.append(80, 90)
TypeError: append() takes
print(Lst)
exactly one argument (2 given)
Code: Output:
Lst = [‘I’,’N’,’D’,’I’,’A’] [‘I’,’N’,’D’,’I’,’A’,’Z’]
Lst.append(‘Z’)
print(Lst)
o Note: if we pass any element which is not in the list then index function will return an error.
Code: Output:
Lst = [ 10, 20, 30, 40, 50 ]
Lst[2]=100
print(Lst) [10, 20, 100, 40, 50]
Lst[3]=’India’
[10, 20, 100, ‘India’, 50]
print(Lst)
Code: Output:
Lst = [ 10, 20, 30]
Lst.extend([40])
print(Lst) [10, 20, 30, 40]
Lst.extend([80, 90])
[10, 20, 30, 80, 90]
print(Lst)
Lst.extend([‘a’,’b’]) [10, 20, 30, 80, 90,’a’,’b’]
print(Lst)
Code: Output:
Lst1 = [ 10, 20, 30]
Lst2 = [ 40,50]
print(Lst1) [ 10, 20, 30]
[ 40,50]
print(Lst2)
[ 10, 20, 30, 40, 50]
Lst1.extend(Lst2)
[ 40,50]
print(Lst1)
print(Lst2)
Lst1.extend(60) TypeError: 'int' object is not
print(Lst1) iterable.
Code: Output:
print(Lst1) [10, 20, 30, 40]
print(Lst2) [80, 90]
L1.append(50)
print(Lst1) [10, 20, 30, 40, 50]
print(Lst2) [80, 90]
L1.append(60,70) TypeError: append() takes
print(Lst1) exactly one argument (2 given)
L1.append([60,70]) [10, 20, 30, 40, [60,70]]
print(Lst1)
L2.extend([100]) [80, 90,100]
print(Lst2)
L2.extend(200) TypeError: 'int' object is not
print(Lst2) iterable
L1.extend([60,70]) [10, 20, 30, 40, 50, 60, 70]
print(Lst1)
Code: Output:
Lst = [ 10, 20, 40]
Lst.insert(2,30)
print(Lst) [10, 20, 30, 40]
To insert at Last
Lst.insert(len(Lst),100) [5, 10, 20, 30, 40, 100]
print(Lst)
o Example:
Code: Output:
Lst = [ 10, 20, 30, 40, 50, 60, 70, 80, 90, 100 ]
del Lst[2]
print(Lst) [10, 20, 40, 50, 60, 70, 80, 90,
100]
del Lst[-1]
print(Lst)
[10, 20, 40, 50, 60, 70, 80, 90]
del Lst[2:6]
print(Lst) [10, 20, 80, 90]
del Lst[15]
IndexError: list assignment
print(Lst)
index out of range
Lst = [ 10, 20, 30, 40, 50, 60, 70, 80, 90, 100 ] [10, 20, 40, 50, 60]
del Lst[4:]
print(Lst)
Lst = [ 10, 20, 80, 90] Deletes all elements…
del Lst NameError: name 'l' is not
print(Lst) defined
Lst = [ 10, 20, 80, 90]
del Lst[:] [] // Empty List
print(Lst)
Lst = [ 1,2,3,4,5,6,7,8,9,10 ]
del Lst[0:10:2] [2, 4, 6, 8, 10]
print(Lst)
Lst = [ 1,2,3,4,5,6,7,8,9,10 ]
del Lst[::3] [2, 3, 5, 6, 8, 9]
print(Lst)
o Note: If we use del items then it will delete all the elements and the list too i.e. now no list
with the name items will exist in python.
Lst.remove(30) 30
print(Lst) [10, 20, 30, 50, 30]
It remove the first occurrence of given item from the
list
Lst.remove(100) ValueError: list.remove(x).x not in list
print(Lst)
Lst.remove() TypeError: remove() takes exactly one argument (0
print(Lst) given)
o Note: unlike ‘del listname’ statement, clear() will removes only the elements and not the list.
After clear() the list object still exists as an empty list.
copy()
o The copy() method returns a shallow copy of the list.A list can be copied using the = operator.
o Syntax : <New_List> = <Old_List>.copy()
Code: Output:
L1=[10, 20, 30, 40, 50]
L2 = L1.copy()
print(L1) [10, 20, 30, 40, 50]
print(L2) [10, 20, 30, 40, 50]
count()
o This function returns the count of the item that you passed as an argument. If the given item is
not in the list, it returns zero.
o OR The count() method returns the number of times element appears in the list.
o Syntax : < List_object >.count(element)
Here, element - the element to be counted.
o Example: Lst=[10, 20, 30, 10, 50, 10, 60, 10, 10]
Code: Output:
print(Lst.count(10)) 5
print(Lst.count(20)) 1
print(Lst.count(100)) 0
reverse()
o This function reverses the items in the list. This is done in place, i.e. It will not create a new list.
o Syntax : <List_object>.reverse()
Code: Output:
Lst=[10, 20, 30, 40, 50]
print(Lst) [10, 20, 30, 40, 50]
Lst.reverse() [50, 40, 30, 20, 10]
print(Lst)
Lst= [‘c,’d’,’e’,’b’,’a’] Output:
print(Lst) [‘c,’d’,’e’,’b’,’a’]
Lst.reverse()
print(Lst) [‘a’, ’b’, ’e’, ’d’, ’c’]
L1=[1, 2, 3] Output:
L2=L1.reverse()
print(L1) [3, 2, 1]
print(L2) None
sort()
o This function sorts the items of the list, by default increasing order. This is done «in place» i.e.
It does not create new list.
o Syntax : <List_object>.sort() // By default increasing order (Ascending order)
<List_object>.sort(reverse=True) //By default decreasing order (Descending order)
Code: Output:
Lst= [10, 50, 40, 30, 20]
print(Lst) [10, 50, 40, 30, 20]
Lst.sort()
print(Lst) [10, 20, 30, 40, 50]
Lst= [10, 50, 40, 30, 20] Output:
print(Lst) [10, 50, 40, 30, 20]
Lst.sort(reverse=True)
print(Lst) [50, 40, 30, 20, 10]
Lst= [‘c,’d’,’e’,’b’,’a’] Output:
print(Lst) [‘c,’d’,’e’,’b’,’a’]
Lst.sort(reverse=True)
print(Lst) [‘a’, ’b’, ’c’, ’d’, ’e’]
L1=[3, 1, 4, 2] Output:
L2=L1.sort()
print(L1) [1, 2, 3, 4]
print(L2) None
min()
o Return item with minmum value in the list.
o Syntax : min(List_Seq)
max()
o Return item with maximum value in the list.
o Syntax : max(List_Seq)
Code: Output:
Lst=[10, 20, 30, 40, 30, 50, 30] 50
print(max(Lst))
list()
o Converts a tuple, string, set, dictionary into list.
o Syntax : list(Seq)
o Example:
Code: Output:
Lst=(10, 20, 30, 10, 50, 10, 60, 10) [10, 20, 30, 10, 50, 10, 60,10]
l1=list(Lst)
print(l1)
Str=”INDIA” ['I', 'N', 'D', 'I', 'A']
s=list(Str)
print(s)
Tuples
Introduction :
o TUPLE is a collection of items and each item has its own index value.
o Tuple is a standard data type of Python. It is a sequence which can store values of any kind.
Forward indexing
0 1 2 3 4 5 6 7 8 9 Positive Index
70 10
10 20 30 40 50 60 80 90 Values
0
-10 -9 -8 -7 -6 -5 -4 -3 -2 -1 Negative Index
Backward indexing
Creating & Initializing Tuple:
Creating a tuple
Tuples are enclosed in parentheses () and each item is separated by a comma.
Syntax: TupleName = ( )
o Creating tuple from existing sequences (i.e. Using tuple() function…
Syntax: TupleName = tuple(sequence)
Code: Output:
>>>Mytuple = tuple(‘Hello’) (‘H’,’e’,’l’,’l’,’o’)
>>>Mytuple
Code: Output:
>>>Mytuple = [‘H’,’e’,’l’,’l’,’o’] (‘H’,’e’,’l’,’l’,’o’)
>>>Mytuple1 = tuple(Mytuple)
>>>Mytuple1
Initializing a tuple
Passing value in tuple while declaring tuple is initializing of a tuple.
Syntax: TupleName = ( value1, value2,…..)
0 1 2 3 4 5 6 7 8 9
10 20 30 40 50 60 70 80 90 100
-10 -9 -8 -7 -6 -5 -4 -3 -2 -1
Code: Output:
>>> n=( 10,20,30,40,50,60,70,80,90,100)
>>>n[1] 20
>>>n[5] 60
>>>n[-3] 80
>>>n[15] Index Error
>>>n[3:8] (40,50,60,70)
Code: I
tpl = (‘I’,’N’,’D’,’I’,’A’) N
for i in tpl: D
print(i) I
A
Code: Output:
tpl = (‘I’,’N’,’D’,’I’,’A’) Index 0 and the element at index -5 is I
l=len(lst) Index 1 and the element at index -4 is
for i in range(0,l): N
print(“Index “,i,” and the element at index“, Index 2 and the element at index -3 is
(i-l),”is”, tpl (i)) D
Index 3 and the element at index -2 is I
Index 4 and the element at index -1 is
A
Tuple operators
o There are many operations that can be performed with Tuple which makes it one of the most
used data types in Python.
o Two basic operators + and * are allowed
+ is used for concatenation (joining)
* Is used for replication (repetition)
Syntax:
<Tuple1> + <Tuple2>
Example:
(10,20,30) + (40,50) (10,20,30,40,50)
(‘I’) + (“Love”) + (“India”) (‘I’,“Love”,“India”)
Code: Output:
>>> T1=( 10,20,30,40,50)
>>>T2=(60,70,80,90,100) (10,20,30,40,50,60,70,80,90,100)
>>>T3 = T1 + T2
>>>T3
Note: you can only add tuple with another tuple not with int, float, complex, or string
type.
(10,20,30) + 5 Invalid
Example:
(10,20,30) * 3 (10,20,30,10,20,30,10,20,30)
(‘I’,“Love”,“India”) *2 (‘I’,“Love”,“India”,‘I’,“Love”,“India”)
Note: you cannot multiply tuple and tuple using *. Only number*tuple or tuple*number is
allowed.
(10,20,30) * (40,50,60) Invalid
Tuple Slicing
o As we know slice means “part of”, so slicing is a process of extracting part of tuple.
o OR Slicing is a part of a tuple containing some continuous character from the tuple.
o The tuple are sliced using a range of indices (Tuple characters have their unique index
position from 0 to length-1 and -1 to –length(backward)).
o Syntax: seq = tuple ( start : stop ) OR seq = tuple ( start : stop : step )
Where start & stop are integer, and start is staring index value, stop is ending index
value & step is stepping index.
o Example1: Tpl1 = ( 10 , 20 , 30 , 40 , 50 , 60 , 70 , 80 , 90 )
Tpl1[: ] / Tpl1[ : :] / Tpl1 ( 10, 20, 30, 40, 50, 60, 70, 80, 90 )
Tpl1[ 2 : 5 ] ( 30, 40, 50 )
Tpl1[ 3 : -3 ] ( 40, 50, 60 )
Tpl1[ 0 : 10 : 2 ] ( 10, 30, 50, 70, 90 )
Tpl1[ : : 3 ] ( 10, 40, 70 )
Tpl1[ : : -1 ] ( 90, 80, 70, 60, 50, 40, 30, 20, 10 )
Tpl1[ : : -2 ] ( 90, 70, 50, 30, 10 )
Tpl1[ -4 : -1 ] ( 60, 70, 80 )
Tpl1[ : : 3 ] ( 10, 40, 70 )
Tpl1 [ 4 ] ( 50 )
Tpl1[ 15 ] IndexError: tuple index out of range
Unpacking tuples
o Creating a tuple from a set of values is called packing and its reverse i.e. creating individual
values from tuple‟s elements is called unpacking.
o Syntax: var1, var2, var3, … = tuple_Object
o Example:
Tpl1 = (100,200,300,400,500)
a,b,c,d,e=Tpl1
print(a) 100
print(b) 200
print(c) 300
print(d) 400
print(e) 500
index(substring) :
o This function returns index position of substring.
o If substring not it returns error ‘substring not found’
o OR This function is used to get the index of first matched item from the tuple. It returns
index value of item to search
o Syntax : <Tuple_Object>.index(substring) :
o Example:
Code: Output:
Tpl=(10,20,30,40,50,60,70,80,90,100) 4
print(Tpl. index (50))
Code: Output:
Tpl=(10,20,30,40,50,60,70,80,90,100) ValueError: tuple.index(x): x
print(Tpl. index (200)) not in tuple.
o Note: if we pass any element which is not in the tuple then index function will return an
error.
len(string) :
o This function returns the number of characters in any tuple including spaces.
o Syntax : len( <Tuple_Object> )
o Example:
Code: Output:
Tpl=(10,20,30,40,50,60,70,80,90,100) 10
print(len(Tpl))
Code: Output:
Tpl=(‘I’,’N’,’D’,’I’,’A’) 5
print(len(Tpl))
o Example:
Code: Output:
Tpl = ( 10, 20, 30, 40, 50, 60, 70, 80, 90, 100 )
del Tpl(2) TypeError: 'tuple' object doesn't
print(Tpl) support item deletion.
Error shown because deletion of a
single element is also possible
Tpl = ( 10, 20, 80, 90) Deletes all elements…
del Tpl NameError: name 'Tpl' is not
print(Tpl) defined
Complete tuple has been deleted. Now
error shown on printing of tuple.
count()
o This function returns the count of the item that you passed as an argument. If the given item is
not in the tuple, it returns zero. OR The count() method returns the number of times element
appears in the tuple.
o Syntax : < Tuple_object >.count(element)
Here, element - the element to be counted.
o Example: Tpl=(10, 20, 30, 10, 50, 10, 60, 10, 10)
Code: Output:
print(Tpl.count(10)) 5
print(Tpl.count(20)) 1
print(Tpl.count(100)) 0
sorted()
o This function sorts the items of the tuple, by default increasing order. This is done «in place»
i.e. It does not create new tuple.
o Syntax : <Tuple1>.sorted(Tuple2) // By default increasing order (Ascending order)
Code: Output:
T1= (10, 50, 40, 30, 20)
print(T1) (10, 50, 40, 30, 20)
T2.sorted(T1)
print(T2) (10, 20, 30, 40, 50)
min()
o Returns item from the tuple with min value.can be done via list
o Syntax : min(Tuple_Seq)
o Example:
Code: Output:
T=(10, 20, 30, 40, 30, 50, 30) 10
print(min(T))
max()
o Returns item from the tuple with max value.can be done via list.
o Syntax : max(Tuple_Seq)
o Example:
Code: Output:
T=(10, 20, 30, 40, 30, 50, 30) 50
print(max(T))
tuple()
o Converts a list into tuple.
o Syntax : tuple(Seq)
o Example:
Code: Output:
T=[10, 20, 30, 10, 50, 10, 60, 10] (10, 20, 30, 10, 50, 10, 60,10)
T1=tuple(T)
print(T1)
Str=”INDIA” ('I', 'N', 'D', 'I', 'A')
s=tuple(Str)
print(s)
sum()
o sum of tuple elements can be done via list
o Syntax : sum(list(Seq))
o Example:
Code: Output:
T=(10, 20, 30]) Sum= 60
T1= sum(list(T)
print(“Sum =”,T1)
Dictionaries
Introduction:
o Python provides us various options to store multiple values under one variable name.
o Dictionaries are also a collection like a string, list and tuple.
o It is a very versatile data type.
o There is a key in it with a value.(KEY:VALUE)
o Dictionaries are mutable data types and it is an unordered collection in the form of
KEY:VALUE
o In List, index of a value is important whereas in dictionary, key of a value is important.
o It is an unordered collection of items where each item consists of a key and a value.
o It is mutable (can modify its contents) but Key must be unique and immutable
Creating a Dictionary
o It is enclosed in curly braces {} and each item is separated from other item by a comma(,).
Within each item, key and value are separated by a colon (:).
o Passing value in dictionary at declaration is dictionary initialization.get() method is used to
access value of a key.
o Syntax:
<dictionary-name>={ <key1>:<value1>,<key2>:<value2>,<key3>:<value3>. . . }
o Creating
Dict1 = {} # empty dictionary
DaysInMonth={"Jan":31,"Feb":28,"Mar":31,"Apr":31,"May":31,"Jun":30,"Jul":31,
"Aug":31,"Sep":30,"Oct":31,"Nov":30,"Dec":31}
o Note: if you try to access “key” which is not in the dictionary, python will raise an error.
Traversing a Dictionary
o It means accessing & processing each & every element of dictionary.
o Python allows to apply “for” loop to traverse every element of dictionary based on their
“key”. For loop will get every key of dictionary and we can access every element based on
their key.
o Syntax : for <item> in <dictionary>:
Code: Output:
stud = dict(( ( “Name”, “Ravi”), (“Cource”,”XI”) )) {“Name”: “Ravi”, “Cource”:”XI”}
stud
stud[“fees”]=45000 {“Name”: “Ravi” , “Cource”:”XI”,
stud “fees”:45000}
stud[“Subject”]=“C.Sc”
stud {“Name”: “Ravi” , “Cource”:”XI”,
“fees”:45000, “Subject“ : “C.Sc“}
o Using pop():
It returns the deleted value after deletion.
Syntax : <dictionary>.pop(<key>)
Code: Output:
emp={“Ravi” : 45000, “Nagu” : 50000, “Roki” : 30000}
emp.pop(“Ravi”) 45000
print(emp) # Value returned after deletion. { “Nagu” ; 50000,
emp.pop(“Ravi”,”Not Found”) “Roki” : 30000}
#If key does not match, given message will be printed. Not Found
o Using clear():
It is used to remove all elements from the dictionary.
Syntax : <dictionary>.clear(<key>)
Code: Output:
emp={“Ravi” : 45000, “Nagu” : 50000, “Roki” : 30000} {“Ravi” : 45000, “Nagu” ;
print(emp) 50000, “Roki” : 30000}
emp.clear() {}
o get() :
This method is used value of given key, if key not found it raises an exception.
OR It is used to get/read the item with the given key. Similarly dict[key]=value. If key
not present it returns error (Key is not defined).
Syntax : <dictionary> . get (key , [default] )
Code: Output:
emp={“Ravi” : 45000, “Nagu” : 50000, “Roki” : 30000}
emp.get(“Nagu”) 50000
emp.get(“Janu”,”Not Found”) Not Found
o items() :
This method returns all the items in the dictionary s a sequence of (key,value) tuple.
OR It is used to get/read the item with the given key. Similarly dict[key]=value. If key
not present it returns error (Key is not defined).
Syntax : <dictionary> . items ()
Code: Output:
emp={“Ravi” : 45000, “Nagu” : 50000, “Roki” : 30000} ('Ravi', 45000)
mylist=emp.items() ('Nagu', 50000)
for k in mylist : ('Roki', 30000)
print (k)
o keys() :
This method return all the keys in the dictionary as a sequence of keys(not in list form).
Syntax : <dictionary> . keys()
Code: Output:
emp={“Ravi” : 45000, “Nagu” : 50000, “Roki” : 30000} ['Ravi', 'Nagu', 'Roki']
emp.keys()
o values() :
This method return all the values in the dictionary as a sequence of keys(a list form).
Syntax : <dictionary> . values()
Code: Output:
emp={“Ravi” : 45000, “Nagu” : 50000, “Roki” : 30000} [45000, 50000, 30000]
emp.values()
o Update() method :
This function merges the dictionary into other dictionary.
This method merges the key:value pair from the new dictionary into original dictionary,
adding or replacing as needed. The items in the new dictionary are added to the old one
and override items already with the same keys.
Syntax : <dictionary-1> . update(<dictionary-2>)
Code: Output:
emp1={“Name” : “Ravi”, “Salary” : 50000, “Age” : 30}
emp2={“Name” : “Nagu”, “Salary” : 55000, “Dept” : “AO”} {“Name” : “Nagu”,
emp1.update(emp2) “Salary” : 55000,
print(emp1) “age””30, “Dept” : “AO”}
print(emp2)
{“Name” : “Nagu”,
“Salary” : 55000,
“Dept” : “AO”}
o copy() :
It will create a copy of dictionary OR returns a shallow copy of the dictionary.
Syntax : <dictionary-2> =(<dictionary-1>).copy()
Code: Output:
emp1={“Name” : “Ravi”, “Salary” : 50000, “Age” : 30} {“Name” : “Ravi”, “Salary” :
emp2={} 50000, “Age” : 30}
emp2=emp1.copy()
print(emp1) {“Name” : “Ravi”, “Salary” :
print(emp2) 50000, “Age” : 30}
o popitem()
It will remove the last dictionary item are return key, value.
Syntax : <key>,<value> . <dictionary>.popitem()
Code: Output:
emp1={“Name” : “Ravi”, “Salary” : 50000, “Age” : 30} “Age” : 30
k, v =emp1.popitem()
print(k,v)