How can I create sets for every item in my list?
This is my list:
listofstrings = ['sdfasdf', 'sdfaserth', 'wegoiog']
I want it to create sets like this:
set1 = set(), set2=set(), set3=set()
How can I create sets for every item in my list?
This is my list:
listofstrings = ['sdfasdf', 'sdfaserth', 'wegoiog']
I want it to create sets like this:
set1 = set(), set2=set(), set3=set()
You can create a list of them like this
sets = []
listofstrings=['sdfasdf', 'sdfaserth', 'wegoiog']
for string in listofstrings:
sets.append(set())
I would use a dictionary to do it(so you can access each set matching the corresponding string).
name_to_set = {}
listofstring = ['sdfasdf', 'sdfaserth', 'wegoiog']
name_to_set = {name: set() for name in listofstring}
so later you can use it as follows:
name_to_set['sdfasdf'].add('x')
You can create as following:
for index, word in enumerate(listofstrings):
exec("set%d = %s" % (index + 1, set()))
As I mentioned, this question is unclear and probably a case of the XY problem. I feel the need to share a solution because so many of the current ones are bizarre at best.
list_of_strs = ['sdfasdf', 'sdfaserth', 'wegoiog']
sets_list = [set() for _ in list_of_strs]
Notice I changed the variable names. In Python, variable names should generally follow the lower_case_with_underscores
style.