0
records = [["chi", 20.0],["beta", 50.0],["alpha", 50.0]]
a = len(records)
for i in range(3):
    var = records[i]
print(var)

What I want to do is to create a different variable for each list (for instance there are three lists in records so i want to assign each list to a different variable (3 variables since there are three lists)). However, it only prints the last list(["alpha", 50.0]) and only one variable is assigned (var).

6
  • 1
    Create a list and append results to it. Commented Mar 8, 2021 at 11:57
  • 1
    Does this answer your question? How do you create different variable names while in a loop? Commented Mar 8, 2021 at 11:59
  • @Maroun--how does"create a list and append results to it" answer OP's intent to "create a different variable for each list" (actually sublist--which is a bad idea)? OP should just use a dictionary with the keys as desired variable names and sublists as the values. Commented Mar 8, 2021 at 12:04
  • 1
    you should re-consider if that's what you really want to do, seems like a bad practice. But perhaps var1, var2, var3 = records. Commented Mar 8, 2021 at 12:04
  • You are assigning values inside a loop and printing outside. Value is assigned to same variable 3 times. So you are seeing last value result. You need 3 different variables or a list so that you can keep values of all three. Commented Mar 8, 2021 at 12:06

1 Answer 1

2

because var is not appending anything it's just overwriting the value in Var but there isn't much point in appending it to separate lists as its all in one list already and everything is accessible by using

records[X][Y]

with X being the sublist location and Y being the value inside that sublist

and if you want this to be easier to use i'll suggest using dictionaries but this isn't necessary

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.