Problem
I am trying to add() elements in a set at run time using a for loop:
l1=set(map(int, input().split()))
n=int(input())
l2=set()
for i in range(n):
l2.add([int, input().split()])
print(l1)
print(l2)
Surprisingly, l1 is a set but, when I go on add() -ing elements to my set l2 in a loop I get :
TypeError: unhashable type: 'list'
Research Effort:
Here are other ways I have tried to add() elements to set l2 and failed :
l2=set()
for i in range(n):
l2.add(map(int, input().split()))
The above prints out :
{<map object at 0x000001D5E88F36A0>, <map object at 0x000001D5E8C74AC8>}
Even this does not work!!
for i in range(n):
l2.add(set(map(int, input().split())))
Please feel free to point out what I am doing wrong.
Basically, an answer will be helpful if one can explain how to add elements to a set data structure at runtime in a loop
Clarification:
I am looking for making a set of sets with user inputs at run time:
So if the user gives the following input:
1 2 3 4 5 6 7 8 9 10 11 12 23 45 84 78
2
1 2 3 4 5
100 11 12
The first line is my set l1. The second line is the number of sets and so since it is 2, the line afterwards are contents of the set.
Expected output:
{{1,2,3,4,5},{100,11,12}}
(int, tuple(input.split())). Mutable objects don't have hashcodes (since they would change when mutated and it would break how hash collections work).setis implemented using a hashtable so it can store only immutable stuff (or at least: stuff whose identity is immutable).lists are mutable hence don't have a hashcode.splitalso returns a list.input().split()the missing parenthesis were a typo