-2

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()
phwt
  • 1,356
  • 1
  • 22
  • 42
ethtrhaef
  • 57
  • 5
  • 1
    `[set(i) for i in listofstrings]` ? – phwt Nov 27 '19 at 06:08
  • 1
    You want to create empty sets for every element in the list. That makes no sense – kuro Nov 27 '19 at 06:11
  • I agree with @kuro, as it is currently presented this question is nonsensical. – AMC Nov 27 '19 at 06:22
  • To follow up on my comment, I think the most likely scenario is that OP is very new to programming and this is a case of the [XY problem](https://meta.stackexchange.com/q/66377/628382). – AMC Nov 27 '19 at 06:42
  • Does this answer your question? [How do I create a variable number of variables?](https://stackoverflow.com/questions/1373164/how-do-i-create-a-variable-number-of-variables) – AMC May 09 '20 at 18:55

4 Answers4

0

You can create a list of them like this

sets = []
listofstrings=['sdfasdf', 'sdfaserth', 'wegoiog']
for string in listofstrings:
    sets.append(set())
Maxxik CZ
  • 314
  • 2
  • 9
0

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')
nonamer92
  • 1,887
  • 1
  • 13
  • 24
0

You can create as following:

for index, word in enumerate(listofstrings):
    exec("set%d = %s" % (index + 1, set()))
  • 1
    Why would you use `exec()` for this!? – AMC Nov 27 '19 at 06:24
  • @ Alexander Cécile Because he wanted different variables for each words not list or dict. You can share your answer without exec(). I would like to see. – Tamoor Salah-U-Din Nov 27 '19 at 06:25
  • If OP does want to dynamically create a bunch of variables, then this is clearly a case of the [XY problem](https://meta.stackexchange.com/q/66377/628382). – AMC Nov 27 '19 at 06:28
  • 1
    @Alexander Cécile That is upto ethtrhaef. You can't decide on his behalf what he wants? He stated the input and output and answer satisfy that. – Tamoor Salah-U-Din Nov 27 '19 at 06:34
  • Oh and your `index + 1` could be done away with if you used `enumerate()`’s `start` parameter. – AMC Nov 27 '19 at 06:45
  • 1
    @AlexanderCécile To me it sounds that the answer fits perfectly to the question. It may have been wise to add a warning about using `exec()`, which is a problematic structure in `python` (google it), but I really don't see why we should avoid answering questions, just because maybe the OP really wants something else. – Aryerez Nov 27 '19 at 06:47
  • @AlexanderCécile Actually that part was meant for the answerer (that he is the one who can put a warning in his answer), but you also didn't seem to mind about the usage of `exec()`, apart from that you think it is not what the OP wanted to ask. – Aryerez Nov 27 '19 at 07:17
  • @Aryerez Let me reassure, I **absolutely** mind the use of `exec()` lol – AMC Nov 27 '19 at 07:22
-1

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.

AMC
  • 2,642
  • 7
  • 13
  • 35
  • Why are you creating a list when the output say to generate dynamic variables. Do you know the use of exec() method in python? – Tamoor Salah-U-Din Nov 27 '19 at 06:36
  • @TamoorSalah-U-Din _Why are you creating a list when the output say to generate dynamic variables._ Because as if often the case with these kinds of questions, a list or dictionary is sufficient. The reference QA for this is https://stackoverflow.com/questions/1373164/how-do-i-create-a-variable-number-of-variables. – AMC May 09 '20 at 18:55