banner



How To Initialize Set In Python

In python programming, we use different data structures like lists, tuples, sets, and dictionaries. Oft nosotros create new lists, sets or dictionaries from existing objects in our programs.  In this article, we will report set comprehension and run into how it is used in python to create new sets from existing objects in Python. We will also look at some examples of set comprehension in Python.

Table of Contents

  1. What is fix comprehension in Python?
  2. Syntax for set comprehension in Python
  3. Examples of set comprehension
    1. Create a ready from elements of another set
    2. Filter elements from a prepare based on a status
    3. Delete elements from a ready
    4. Alter the data type of elements of the ready
  4. Conclusion

What is ready comprehension in Python?

Set comprehension is a method for creating sets in python using the elements from other iterables like lists, sets, or tuples. Just like we utilize list comprehension to create lists, we tin can apply set comprehension instead of for loop to create a new set and add elements to it.

Syntax for gear up comprehension in Python

The syntax for ready comprehension is every bit follows.

newSet= { expression for chemical element in  iterable }

Description of the syntax:

  • The iterable may be any iterable object or data structure in Python from which we accept to use the elements to create the new set.
  • The element denotes the element from the iterable that has to be included in the set up.
  • The expression can be any mathematical expression derived from the element.
  • newSet is the name of the new fix which has to be created from the elements of the iterable.

Permit the states discuss this syntax using an example. In the post-obit case, we are given a listing of ten integers. We have to create a fix of the triples of these integers. This can exist done using fix comprehension as follows.

            myList = [1, two, 3, iv, 5, 6, seven, eight, 9, 10] newSet = {element*3 for element in myList} print("The existing listing is:") impress(myList) print("The Newly Created set is:") impress(newSet)                      

Output:

            The existing list is: [i, 2, three, 4, v, half dozen, 7, 8, 9, 10] The Newly Created fix is: {three, 6, ix, 12, fifteen, eighteen, 21, 24, 27, xxx}                      

In the above case, we are given a listing of x integers and nosotros have created a set of triples of the elements of the given list. In the argument "newSet = {chemical element *three for element in myList}", Set comprehension is used to create the newSet which contains triples of the elements in myList .

If you volition compare the code with the syntax for ready comprehension, the post-obit observation can be fabricated.

  1. mySet is the set whose elements have been used to create a new set. Hence mySet has been used in identify of the iterable.
  2. We have created a new set with triples of the elements of myList. Hence, chemical element*3 has been used in place of expression.

We can too use conditional statements in a set comprehension. The syntax for using conditional statements in set comprehension is as follows.

newSet= { expression for chemical element in  iterable if condition }

Description of the syntax:

  • The iterable may be any iterable object or data structure in Python from which we have to apply the elements to create the new gear up.
  • The condition is a provisional expression using
  • The element denotes the element from the iterable that has to be included in the set.
  • The expression tin be any mathematical expression derived from the element.
  • newSet is the proper name of the new set which has to be created from the elements of the iterable.

Let the states hash out this syntax using an instance. In the post-obit example, we are given a listing of 10 integers. We have to create a set of triples of even integers. This tin can be done using ready comprehension every bit follows.

            myList = [ane, 2, iii, four, 5, 6, 7, 8, 9, 10] newSet = {element*3 for element in myList if element % 2 ==0} print("The existing list is:") impress(myList) print("The Newly Created set is:") impress(newSet)                      

Output:

            The existing list is: [1, 2, three, iv, five, 6, 7, 8, 9, 10] The Newly Created set up is: {half-dozen, 12, 18, 24, 30}          

In the higher up example, we have a listing of 10 integers and we have created a set of triples of those elements of the given list which are even numbers. In the argument "newSet = {element *3 for element in myList if element % two == 0}", Set comprehension is used to create the newSet which contains squares of the fifty-fifty elements in myList .

If nosotros compare the code with the syntax for set comprehension, the following observation can be made.

  1. myList is the listing whose elements accept been used to create a new fix. Hence myList has been used in identify of the iterable.
  2. We accept to create a new listing with triples of the even elements of myList. Hence, chemical element*three has been used in place of expression.
  3. To select merely the even elements, we accept used a conditional statement "element % ii == 0" at the identify of status.

Examples of set up comprehension

Now that we have understood the syntax for set up comprehension in python, we will some examples to understand the concept in a better way.

Create a set from elements of another set

If you have to create a gear up using elements of another fix, you can do so by creating a new set. After creating a new set up, you lot can add elements to the new set using add() method and for loop . In the following example, we have created a new set with squares of element of an existing prepare.

            mySet = {1, 2, 3, 4, 5, 6, seven, 8, 9, 10} newSet = set() for element in mySet:    newSet.add(element**2) print("The existing ready is:") print(mySet) print("The Newly Created set is:") print(newSet)                      

Output:

            The existing gear up is: {1, ii, three, 4, 5, vi, 7, eight, 9, 10} The Newly Created set is: {64, ane, 4, 36, 100, 9, xvi, 49, 81, 25}          

In the above case, initializing an empty prepare then using add() method to add together elements to information technology is inefficient. Instead of this, we can directly initialize the new set with all the elements using the set comprehension every bit follows.

            mySet = {ane, 2, iii, 4, 5, half dozen, seven, eight, 9, x} newSet = {element ** 2 for chemical element in mySet} print("The existing gear up is:") print(mySet) print("The Newly Created gear up is:") print(newSet)                      

Output:

            The existing prepare is: {ane, ii, 3, iv, 5, half-dozen, 7, 8, 9, ten} The Newly Created fix is: {64, one, 4, 36, 100, 9, 16, 49, 81, 25}          

Filter elements from a set based on a status

We can create a new ready from elements of an old set up by applying some conditions to the elements. To do this using a for loop, you can use a conditional statement and the add() method every bit follows. In the following example, we have filtered even numbers from a set.

            mySet = {1, 2, iii, 4, v, half-dozen, 7, viii, 9, x} newSet = set() for element in mySet:     if element % two == 0:         newSet.add(element) print("The existing fix is:") print(mySet) print("The Newly Created set is:") print(newSet)                      

Output:

            The existing set is: {1, ii, three, 4, 5, half-dozen, 7, 8, nine, 10} The Newly Created set is: {2, 4, 6, 8, 10}                      

Instead of the for loop, yous tin can filter out elements from old prepare to create a new set using set comprehension as follows.

            mySet = {1, 2, iii, 4, 5, 6, 7, 8, 9, 10} newSet = {chemical element for element in mySet if chemical element % 2 == 0} print("The existing set is:") print(mySet) print("The Newly Created ready is:") print(newSet)                      

Output:

            The existing set is: {1, 2, 3, 4, 5, vi, 7, 8, 9, 10} The Newly Created ready is: {2, 4, half dozen, 8, 10}          

Delete elements from a set

If you have to delete some elements from a ready, you can create a new set from the elements that have not to be deleted. After that, you can assign the new set to the former set variable equally follows. In the post-obit case, nosotros accept deleted all the odd elements from a set.

            mySet = {one, 2, iii, 4, five, 6, 7, 8, 9, 10} print("The existing set is:") print(mySet) mySet = {chemical element for element in mySet if element % two == 0} impress("The modified fix is:") impress(mySet)                      

Output:

            The existing set is: {1, 2, 3, iv, 5, 6, 7, 8, 9, 10} The modified set is: {2, four, 6, 8, ten}          

Change the data type of elements of the set

Nosotros can also change the data types of the elements of a set using set comprehension equally shown in the following example. Here, nosotros have converted all the integer elements of a ready to strings.

            mySet = {i, 2, iii, iv, five, half dozen, 7, viii, nine, x} newSet = {str(element) for element in mySet } impress("The existing gear up is:") print(mySet) print("The Newly Created set is:") print(newSet)                      

Output:

            The existing set is: {1, 2, three, four, 5, 6, 7, viii, ix, x} The Newly Created set is: {'2', '4', '8', 'vi', 'iii', '7', 'ane', '10', '5', 'ix'}          

Conclusion

In this article, nosotros take discussed set comprehension in Python. We also looked at its syntax and examples to understand the concept better.To acquire more virtually other data structures, you can read this article on Linked List in Python.

Recommended Python Training

Form: Python 3 For Beginners

Over xv hours of video content with guided pedagogy for beginners. Acquire how to create real world applications and master the basics.

How To Initialize Set In Python,

Source: https://www.pythonforbeginners.com/basics/set-comprehension-in-python

Posted by: harrismith1999.blogspot.com

0 Response to "How To Initialize Set In Python"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel