How to convert List to Set?Sets and Lists are the data structures in Python that consist of groups of data elements. However, these two data structures have some significant differences too. In specific, the lists perform a few ranges of mathematical operations that are useful to search for particular elements – specifically with duplicates and managing data. In contrast, the sets are helpful to perform a variety of functions like unions and intersections. There are many reasons we have to complete a list to set conversion in Python, and this can be done using a simple function. But before we get started, let us understand some concepts of Lists and Sets. Lists in PythonLists in Python are utilized to store more than one data element in a single variable. Lists are among the four built-in data types in Python used to accumulate collections of data. The rest three includes Sets, Tuples, and Dictionary, and all these data types have different abilities and usage. We can create the Lists using the square brackets. Let us consider the following example to understand the List. Example: Output: ['Apple', 'Mango', 'Banana', 'Orange', 'Guava', 'Mango', 'Strawberry', 'Grapes'] Explanation: In the above example, we have created a list of eight elements using the square brackets enclosing the elements. We have then printed the list for the users. Moreover, we can also observe that one element is repeated in the list, which implies that List elements are ordered, changeable, and allows duplicate values. The indexing of List elements begins with [0] for the first element, [1] for the second element, and so on. Sets in PythonSets are also utilized to store more than one element in a single variable. Sets are among the four core data types in Python that are used to accumulate collections of data. A set is an unordered and unindexed collection that is written with curly brackets. Let us consider the following example to understand Set. Example: Output: {'Strawberry', 'Mango', 'Grapes', 'Pineapple', 'Apple', 'Orange', 'Guava', 'Banana'} Explanation: In the above example, we have created a set of eight elements using the curly brackets enclosing the elements. We have then printed the set for the users. Note: Sets are an unordered collection of data. Hence, we can’t be sure which order the elements will appear.Understanding the difference between Lists and SetsThe most significant difference between a set and a list in Python is that a set only stores unique items, whereas a list can consist of identical elements. For instance, suppose we have a list of mathematic test marks defined as “marks = [25, 30, 21, 19, 25, 27, 25, 17, 23, 20]”, the list displays the user every value; however, when we convert the list into a set, it will remove the duplicates and leave {25, 30, 21, 19, 25, 27, 17, 23, 20}. Another significant difference is that the sets use curly brackets, whereas the lists use square brackets. Note: Things might get confusing as the dictionary in Python also utilizes curly brackets. However, each data element consists of the value itself and colon separating a “key”.It is a point to remember that we can utilize integers, strings, floats, or other objects as a fragment of a set or a list and mixed them in the same list or set. Thus, similar to having a set of integers in Python, we can also create a set of strings in Python and a lot more. Converting a List into a SetThe process of converting a list into a set in Python is easy and simple. At first, we have to define a list. For instance, suppose we have a list of few names defined as the_names = [“George”, “Josh”, “James”, “Mark”, “Carlo”, “James”, “Andy”, “Sara”, “Andy”, “Victor”]. We can convert the list into a set using the set() command, where we have to insert the list name between the parentheses that are needed to be converted. Hence, in the above case, we have to type the set(the_names) in order to convert the names, present in the list into a set. Moreover, we can assign this function to a variable such as “the_unique_names” and show the result to users. One can also use print( set( the_names)) function to make it more efficient. Let us understand the same using the following example: Example: Output: {'Victor', 'George', 'Josh', 'Andy', 'Mark', 'Carlo', 'Sara', 'James'} Explanation: In the above example, we have defined the list as the_names containing ten data elements. We have then defined a variable as the_unique_names that use the set() command to convert the list into the set. At last, we have printed the set. As a result, the list has been converted to the set successfully. We can also observe that the names such as James and Andy have been printed only once. |
原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/tech/courses/263548.html