{"id":3323,"date":"2023-10-21T17:09:06","date_gmt":"2023-10-21T11:39:06","guid":{"rendered":"https:\/\/python-programs.com\/?p=3323"},"modified":"2023-11-10T11:51:56","modified_gmt":"2023-11-10T06:21:56","slug":"7-ways-to-add-all-elements-of-list-to-set-in-python","status":"publish","type":"post","link":"https:\/\/python-programs.com\/7-ways-to-add-all-elements-of-list-to-set-in-python\/","title":{"rendered":"7 Ways to add all elements of list to set in python"},"content":{"rendered":"
As we know a Example of a set<\/p>\n Where,<\/p>\n Similarly, a Example of list<\/p>\n where,<\/p>\n When we will add all the items of the list to set it will look like this<\/p>\n Here, all the items are unique after adding as because list contains unique elements.<\/p>\n In the above program we just passed the list inside the update( ) method, the elements which are not present in set those got added and the elements which are present in set those got skipped as set contains only unique elements.<\/p>\n But the thing is that, add( ) only accept single element inside that. So, if we will pass list in that it will give So to avoid this error we can use for loop and pass one by one element of the list inside We can add content of two sets by using By creating union of 2 sets by using We will add the elements of list to set just like previous solution means we will convert list to set then we will add the elements by finding union of two sets.<\/p>\n But we will convert list to set by string literal and unpacking our lists elements inside it.<\/p>\n Suppose we have 2 different list then add the elements of both the list to set.<\/p>\n How to add all elements of list to set in python ? As we know a set in python is a built-in\u00a0 data type which stores multiple unordered items in a single variable. Example of a set my_set = {10,20,30,40} Where, A set contains unique values. All items placed inside { }. Similarly, a list …<\/p>\nset<\/code> in python is a built-in\u00a0 data type which stores multiple unordered items in a single variable.<\/p>\n
my_set = {10,20,30,40}<\/pre>\n
\n
list<\/code> in python is a built-in\u00a0 data type which is used to store items of different types.<\/p>\n
my_list = {30,40,50,60,70}<\/pre>\n
\n
{10,20,30,40,50,60,70}<\/pre>\n
Method-1 : Add all elements of a list to set using update( ) function<\/h3>\n
Set class<\/code> provides an
update( )<\/code> method with the help of which all elements of list can be added to set.<\/p>\n
Syntax : set.<\/span>update<\/span>(<\/span>sequences<\/span>)<\/span><\/pre>\n
#Program :\r\n \r\n#Represents a set\r\nmy_set = {10, 20, 30, 40}\r\n#Represents a list\r\nmy_list = [30, 40, 50, 60, 70, 80]\r\n# adding all elements in list to the set\r\nmy_set.update(my_list)\r\nprint('After adding list to set : ')\r\nprint(my_set)<\/pre>\n
Output :\r\nAfter adding list to set : \r\n{10,20,30,40,50,60,70,80}<\/pre>\n
Method-2 : Add all elements of a list to set using add( ) function<\/h3>\n
Set class<\/code> provides an
add( )<\/code> method with the help of which all elements of list can be added to set.<\/p>\n
Syntax : set.<\/span>add<\/span>(<\/span>element<\/span>)<\/span><\/pre>\n
TypeError: unhashable type: <\/code><\/span>‘list’ as output.<\/span><\/p>\n
add( )<\/code> method.<\/p>\n
#Program :\r\n\r\n#Represents a set \r\nmy_set = {10, 20, 30, 40} \r\n#Represents a list \r\nmy_list = [30, 40, 50, 60, 70, 80] \r\n# adding all elements in list to the set \r\n# By iterating over all elements of list using for loop\r\nfor item in my_list:\r\n # adding each element to the set\r\n my_set.add(item)\r\nprint('After adding list to set : ') \r\nprint(my_set)<\/pre>\n
Output : \r\nAfter adding list to set : \r\n{10,20,30,40,50,60,70,80}<\/pre>\n
Method-3 : Add a list to set using add() & union()<\/h3>\n
union( )<\/code> method.<\/p>\n
Syntax : s.<\/span>union<\/span>(<\/span>t<\/span>)<\/span><\/pre>\n
#Program : \r\n\r\n#Represents a set \r\nmy_set = {10, 20, 30, 40} \r\n#Represents a list \r\nmy_list = [30, 40, 50, 60, 70, 80] \r\n#convert list to set\r\n#then add contents two sets \r\nmy_set = my_set.union(set(my_list)) \r\nprint('After adding list to set : ') \r\nprint(my_set)<\/pre>\n
Output : \r\nAfter adding list to set : \r\n{10,20,30,40,50,60,70,80}<\/pre>\n
Method-4 : Add all elements in a list to set using | operator<\/h3>\n
| operator<\/code> , then we can add elements of two sets. So we have to convert our list to set by using
| operator<\/code>.<\/p>\n
#Program :\r\n\r\n#Represents a set \r\nmy_set = {10, 20, 30, 40} \r\n#Represents a list \r\nmy_list = [30, 40, 50, 60, 70, 80] \r\n# adding all elements in list to the set \r\n# by converting list to set \r\n# then get union of both the sets using | operator\r\nmy_set |= set(my_list )\r\n\r\nprint('After adding list to set : ') \r\nprint(my_set)<\/pre>\n
Output : \r\nAfter adding list to set : \r\n{10,20,30,40,50,60,70,80}<\/pre>\n
Method-5 : Add a list to set using |= and unpacking list to set<\/h3>\n
#Program : \r\n\r\n#Represents a set \r\nmy_set = {10, 20, 30, 40} \r\n#Represents a list \r\nmy_list = [30, 40, 50, 60, 70, 80] \r\n# adding all elements in list to the set \r\n# by converting list to set \r\n# then get union of both the sets using | operator \r\n# Unpacking elements and OR that with original set\r\nmy_set |= set(*my_list ) \r\nprint('After adding list to set : ') \r\nprint(my_set)<\/pre>\n
Output : \r\nAfter adding list to set : \r\n{10,20,30,40,50,60,70,80}<\/pre>\n
Add all elements from multiple lists to the set :<\/h3>\n
#Program : \r\n\r\n#Represents a set \r\nmy_set = {10, 20, 30, 40} \r\n\r\n#Represents a list \r\nmy_list1 = [30, 40, 50, 60, 70] \r\nmy_list2= [80,90] \r\n# adding all elements in list to the set \r\nmy_set.update(my_list1,my_list2) \r\nprint('After adding list to set : ') \r\nprint(my_set)<\/pre>\n
Output : \r\nAfter adding list to set : \r\n{10,20,30,40,50,60,70,80,90}<\/pre>\n","protected":false},"excerpt":{"rendered":"