1 00:00:00,120 --> 00:00:04,380 Hello and welcome to this session on Python and six 2 00:00:07,300 --> 00:00:16,730 said is an on our door collection of data with no duplicate elements. 3 00:00:16,730 --> 00:00:30,170 Until now we have talked about different sequences in python like Lis tuples dictionaries as you can 4 00:00:30,170 --> 00:00:31,030 see here. 5 00:00:31,070 --> 00:00:40,700 The elements in a list are the tuple or a dictionary and not unique. 6 00:00:40,700 --> 00:00:50,840 So there aren't duplicate items in all of these data sequences but coming to a set it does not contain 7 00:00:50,900 --> 00:01:03,390 a need duplicate element and the elements in a set of UN ordered unlike lists and tuples because sets 8 00:01:03,390 --> 00:01:07,330 cannot have multiple occurrences of the same element. 9 00:01:07,440 --> 00:01:17,260 It makes it highly useful to remove duplicate values from a list or a tuple next set. 10 00:01:17,370 --> 00:01:30,840 I did a durable and mutable so we can add or remove elements from a set we can also perform common mathematical 11 00:01:30,840 --> 00:01:39,430 operations like unions intersections symmetric differences using set. 12 00:01:39,570 --> 00:01:45,520 We will have a detailed discussion on these operations in the coming up session. 13 00:01:45,570 --> 00:01:55,440 You define a set containing values using curly braces like this and there can be any number of items 14 00:01:55,680 --> 00:02:07,080 in a set and they may be of different types such as integers flawed tuple strings etc. But the elements 15 00:02:07,110 --> 00:02:14,930 in a set can not be a mutable element like a list or another set or a dictionary. 16 00:02:16,200 --> 00:02:22,240 And if you want to define an empty set in Python this is how you do. 17 00:02:22,530 --> 00:02:33,040 Say for example said B is an empty set that gays you can call the in function set like this. 18 00:02:34,440 --> 00:02:45,430 You do not use the curly braces but no values to define an empty set because this is resolved to define 19 00:02:46,030 --> 00:02:58,950 an empty dictionary so here in the Jupiter notebook I have defined a set E and the elements in the set 20 00:02:59,040 --> 00:03:02,250 a are not unique. 21 00:03:02,250 --> 00:03:10,120 So let's execute this line and then display said e as you can see here. 22 00:03:10,140 --> 00:03:13,200 The duplicate elements in set a. 23 00:03:13,200 --> 00:03:23,430 Has been removed because multiple occurrences of an element are not allowed in a set and a set can also 24 00:03:23,430 --> 00:03:30,480 be used to remove duplicate items in a tuple on a list. 25 00:03:30,480 --> 00:03:35,170 So let us pass a list to the built in function set. 26 00:03:35,300 --> 00:03:40,040 We'll look pass the list containing these forward values. 27 00:03:40,170 --> 00:03:52,350 See that the duplicate elements have been removed from the set and in order to define an empty set you 28 00:03:52,860 --> 00:03:56,810 call the built in function set like this. 29 00:03:57,120 --> 00:04:08,180 And then if we check the type of set B that is the data type does it at dawn these data types at say 30 00:04:08,180 --> 00:04:19,230 for example you define another set using the curly braces without any values and then execute this line 31 00:04:19,440 --> 00:04:25,020 and then check the data type offsets see. 32 00:04:25,230 --> 00:04:35,220 So as you can see does return the data type the because your dick empty curly braces are reserved for 33 00:04:35,220 --> 00:04:44,070 defining empty dictionary and then we have also discussed that the elements in a dictionary can be of 34 00:04:44,130 --> 00:04:56,400 any immutable later type such as integers flawed strings tuples but B can not have mutable elements 35 00:04:56,490 --> 00:04:57,660 in a set. 36 00:04:57,660 --> 00:05:02,890 Say for example I now try to change d set E.. 37 00:05:03,320 --> 00:05:05,490 I'll try to add in a list here. 38 00:05:06,720 --> 00:05:09,670 D then execute. 39 00:05:09,780 --> 00:05:12,810 So you have deed type error here. 40 00:05:13,870 --> 00:05:23,530 Python has raised up type error so meaning you cannot have a mutable data type in a set. 41 00:05:23,530 --> 00:05:32,350 So as we have discussed a set is created by placing all the elements inside curly braces separated by 42 00:05:32,350 --> 00:05:41,530 commas or you can use the built in function set we will discuss the different methods that can be used 43 00:05:41,950 --> 00:05:52,930 on set the add method can be used to add elements to a set if you want to add a single element we can 44 00:05:52,930 --> 00:05:54,900 use the ad method. 45 00:05:55,000 --> 00:06:04,060 So using the add method we can add immutable elements to a set. 46 00:06:04,160 --> 00:06:08,560 And here we have called the add method on set E. 47 00:06:09,320 --> 00:06:23,120 Let's display set a so as you can see the element ten point 0 has been added to the set and also the 48 00:06:23,170 --> 00:06:33,110 set to a hill did not retain the order in which we have defined the elements because set at an unordered 49 00:06:33,110 --> 00:06:42,850 collection of data if you want to remove all elements from a set you can use the clear method and then 50 00:06:42,850 --> 00:06:54,670 we have the update method the Update method can take tuples less strings or other set as it argument 51 00:06:55,090 --> 00:06:59,290 and this method removes any duplicate. 52 00:06:59,590 --> 00:07:07,200 So let us add the elements in a list to dissect a. 53 00:07:08,600 --> 00:07:17,050 So using the update methods I am gonna pass a list one to two. 54 00:07:17,390 --> 00:07:23,570 One and then five common nine. 55 00:07:23,810 --> 00:07:37,820 So let's execute this line and then display set a the update method as you can see has included unique 56 00:07:37,910 --> 00:07:51,130 elements from the list in to the set a the list itself is not added as a single element in date set 57 00:07:52,470 --> 00:08:00,040 likewise you can add elements from two different set. 58 00:08:00,120 --> 00:08:03,580 So let's define another set here. 59 00:08:03,600 --> 00:08:07,560 Set B contains elements such as 60 00:08:10,100 --> 00:08:17,910 point eight nine dot or forty five dot o. 61 00:08:18,960 --> 00:08:28,490 So if you want to add elements from say B to set a you can use the update method. 62 00:08:28,650 --> 00:08:32,150 The argument should be said B. 63 00:08:32,510 --> 00:08:35,250 Now letters display set a 64 00:08:38,340 --> 00:08:48,210 so as you can see since all the elements and set B are unique the elements have been added to set a. 65 00:08:48,570 --> 00:08:57,750 Then if you want to remove elements from a set you have the discard method and the remove method. 66 00:08:58,680 --> 00:09:04,180 So using the discard method I'm going to remove one of the elements here. 67 00:09:04,200 --> 00:09:12,270 So I'm going to remove this element and then displace it a say for example the element that you want 68 00:09:12,270 --> 00:09:22,500 to discard from this set is not existing in on set is since a dot eight is removed from the set. 69 00:09:22,500 --> 00:09:31,940 Let's try to discard this element again and as you can see there is no exception raised when you tried 70 00:09:31,940 --> 00:09:42,680 to discard an element which is not existing in a set but coming to the remove method if we tried to 71 00:09:42,680 --> 00:09:52,210 remove the same element a dot aid from said a it is going to raise an exception key error. 72 00:09:52,520 --> 00:10:04,510 And then you also had the pop metal that can be used to remove an arbitrary element from a set so this 73 00:10:04,630 --> 00:10:07,750 element has been removed from the set. 74 00:10:07,750 --> 00:10:11,780 Let's now display the set E. 75 00:10:11,940 --> 00:10:21,870 So as you can see the pop method removes a random item from from a set and then you can use the Lent 76 00:10:21,870 --> 00:10:27,990 function to calculate the length of a set. 77 00:10:28,110 --> 00:10:38,610 So since there are 10 elements the lend function has returned 10 and we have discussed before that a 78 00:10:38,610 --> 00:10:47,640 common use of set and python is computing standard mathematical operations such as union intersection 79 00:10:48,030 --> 00:10:51,560 diffidence and symmetric difference. 80 00:10:51,600 --> 00:10:58,160 So let's discuss about these methods in detail. 81 00:10:58,680 --> 00:11:07,320 So here I have defined to set set C containing the element one two three four and five and then said 82 00:11:07,320 --> 00:11:12,720 D contains the elements two four six eight and 10. 83 00:11:12,720 --> 00:11:25,320 Now I want to find out all of the values which are existing in set C and set D. 84 00:11:25,320 --> 00:11:30,970 So I want to find out all the unique values in these two set. 85 00:11:31,050 --> 00:11:40,170 So that means I want to retrieve these values in this area. 86 00:11:41,480 --> 00:11:47,880 In order to retrieve these values I can use the union method. 87 00:11:49,190 --> 00:11:55,770 Set C union said D. 88 00:11:56,780 --> 00:11:59,240 And this is going to retrieve 89 00:12:01,780 --> 00:12:11,120 one two three four five six eight and 10. 90 00:12:11,470 --> 00:12:18,760 So here in my Jupiter notebook I have defined these two sets set C and set D. 91 00:12:18,760 --> 00:12:31,440 Now I want to find out all the unique values from these two sets so me will use the union method to 92 00:12:31,440 --> 00:12:35,990 retrieve all the unique values from the set. 93 00:12:36,600 --> 00:12:44,700 So I'm going to pass set D as an argument to the union matter and then execute. 94 00:12:44,700 --> 00:12:55,290 As you can see here the union method has returned a set of all of the values that are existing in set 95 00:12:55,290 --> 00:13:00,840 C or said D or both. 96 00:13:00,840 --> 00:13:12,770 Next if you want to find out the elements which are common to set C and said D. 97 00:13:13,620 --> 00:13:24,150 So I'm talking about the values in this area in order to get these values we can use the intersection 98 00:13:24,150 --> 00:13:38,450 method so set C dot intersection and we are going to pass the set D as the argument to this method and 99 00:13:38,450 --> 00:13:40,120 then execute. 100 00:13:40,130 --> 00:13:47,910 So these two elements to unfold add the common elements in these two sets. 101 00:13:48,080 --> 00:13:59,330 Next if I want to retrieve the elements in set C which are not existing in set D that means I want to 102 00:13:59,330 --> 00:14:03,170 retrieve the values in this area. 103 00:14:04,010 --> 00:14:14,810 So in that case I can use the different method in order to the G d values 1 3 and 5 the elements 2 and 104 00:14:14,810 --> 00:14:17,220 4 add existing instead D. 105 00:14:17,390 --> 00:14:20,750 So I do not want to retrieve those values 106 00:14:23,450 --> 00:14:30,560 so said see Daud diffident is the method you pass. 107 00:14:30,560 --> 00:14:40,920 D said D as the argument and this is going to retrieve all of the elements in said C which are not part 108 00:14:41,000 --> 00:14:42,360 of said D. 109 00:14:42,930 --> 00:14:51,810 So as you can see these elements 1 3 5 are unique to set C. 110 00:14:51,810 --> 00:15:00,720 Next say for example I want to retrieve all of the elements that are by use of exactly one of the two 111 00:15:00,720 --> 00:15:03,130 sets but not both. 112 00:15:03,660 --> 00:15:14,060 So I want to retrieve all the elements in this area and this area as well. 113 00:15:14,190 --> 00:15:18,680 So I want to retrieve all these six elements. 114 00:15:19,020 --> 00:15:27,360 And because 2 and 4 are common to set C and set D I do not want to retrieve these two elements. 115 00:15:27,510 --> 00:15:38,340 So if I want to retrieve the values that are existing in one of the two sets but not bored I can use 116 00:15:38,340 --> 00:15:41,490 the method symmetric difference. 117 00:15:41,700 --> 00:15:55,680 So let's call this method symmetric difference so when I execute this say I'm going to retrieve the 118 00:15:55,800 --> 00:16:04,590 elements that are existing in one of the two sets but not put the next method that we're going to talk 119 00:16:04,590 --> 00:16:08,240 about is the is disjoint method. 120 00:16:08,730 --> 00:16:18,750 And this method returns true if the two sets have a null intersection that means both of the sets do 121 00:16:18,750 --> 00:16:22,210 not have any element in common. 122 00:16:22,260 --> 00:16:31,780 So if we call the method is does join and pass these said these is a argument. 123 00:16:31,810 --> 00:16:33,780 This is going to return false. 124 00:16:33,820 --> 00:16:40,150 That's because there are some common elements between these two sets set C and set D. 125 00:16:40,150 --> 00:16:42,820 No let's display said B. 126 00:16:42,820 --> 00:16:50,370 Let us now see is disjoint method returns then we pass said B as an argument. 127 00:16:51,070 --> 00:16:57,660 So said C Dart is disjoint set B. 128 00:16:58,120 --> 00:17:05,800 So this method does return true because there are no common elements between set C and set B 129 00:17:08,530 --> 00:17:12,360 and the next method is subset. 130 00:17:12,580 --> 00:17:17,720 But you don't draw if another set contains this set. 131 00:17:17,800 --> 00:17:29,810 So here I have called the is subset method on set C by passing said D as an argument and this is returned 132 00:17:29,960 --> 00:17:42,680 false because not all of the elements in set C and existing in set D similarly B have the is super set 133 00:17:43,280 --> 00:17:48,580 which returns true if this particular set contains another set. 134 00:17:49,190 --> 00:17:59,180 So I'm gonna call inequality is super set method on set C and I have passed a set containing these two 135 00:17:59,180 --> 00:18:03,250 elements 1 and 2 to this particular method. 136 00:18:03,470 --> 00:18:11,500 And since these elements are existing and set C this method has returned true. 137 00:18:11,660 --> 00:18:19,610 And then we can also perform membership test to check whether a specific element is contained in a set. 138 00:18:19,610 --> 00:18:33,150 So for example I want to check if one is existing in Set C in Set C so because the element 1 is existing 139 00:18:33,150 --> 00:18:35,740 in said see this as return true. 140 00:18:36,060 --> 00:18:43,370 Let's say 1 not in Set C so they should return false. 141 00:18:43,500 --> 00:18:51,900 Next we have the sorted function that can be used to sort the elements in a set. 142 00:18:52,020 --> 00:18:59,850 So we have talked about set and set an unordered collection of data. 143 00:19:00,330 --> 00:19:09,180 But if you need to get these values from your set in an ordered form you can use the sorted function 144 00:19:09,840 --> 00:19:13,260 which outputs a list that is ordered. 145 00:19:13,350 --> 00:19:22,590 So here I have defined set e containing these elements and they add in an unsorted audit. 146 00:19:22,950 --> 00:19:28,520 So if you want to saw the order of the element in Set A. 147 00:19:28,770 --> 00:19:36,060 We can use these altered function so we will look past set into this function and this function sorted 148 00:19:36,090 --> 00:19:42,770 is going to return these elements in ascending order. 149 00:19:42,980 --> 00:19:54,470 And this brings us to the end of the discussion on the method that can be used on sets. 150 00:19:54,730 --> 00:20:03,820 You can either it through a set just like we do for other data sequences such as lists dictionary and 151 00:20:03,820 --> 00:20:14,660 tuples so we can use a for loop to retrieve all of the elements in a set so here I have defined a set 152 00:20:14,780 --> 00:20:18,320 said each containing different elements. 153 00:20:18,500 --> 00:20:24,810 Now using a for loop will print all of the elements in set E.. 154 00:20:24,920 --> 00:20:38,010 So for x in set e meaning for each element and set e print the element so print x. 155 00:20:38,130 --> 00:20:46,950 So as you can see here the order in which these elements have been printed using the for loop is different 156 00:20:46,950 --> 00:20:51,310 from the order in which we have defined set E. 157 00:20:51,370 --> 00:20:57,960 That's because the element in a set are in an on ordered form.