0

Lets say I have a list with n elements like this -

problems = ["This is something", "This is something else", "This is 3rd element", "4", 8]

I want to make 1 list for every element in the given list like this -

List1 = ["This is something"]
List2 = ["This is something else"]
List3 = ["This is 3rd element"]
List4 = ["4"]
List5 = ["8"]

I was thinking of a code that uses a for loop to do this but I could not give the new list a name,

for i in problems:
   problems[i] = f"list{n}"

Is there any way I can do something like this ?

CodeSpec
  • 31
  • 6
  • Also I cannot import any libraries – CodeSpec Jan 26 '22 at 09:47
  • 1
    What are you really trying to achieve? Do **NOT** create variables dynamically. If this is for an assignment, tell your teacher this is a bad practice and you should not learn how to do this. – mozway Jan 26 '22 at 09:48
  • You should use dictionary for this purpose. But If you have a requirement to do this way then you can make use of globals(). – Usama Shahid Jan 26 '22 at 09:49
  • You can create variables like this : for idx, val in enumerate(problems): globals()[f"List{idx+1}"] = list(value0 – Usama Shahid Jan 26 '22 at 09:53

0 Answers0