0

So I have this dataframe Ttrain where the sex column has 2 unique values male and female. What I need to do is run a loop, where the unique values from a column will be extracted and assigned to unique variables. So for sex column, a[1]= male and a[2] = female should be the output. I'm trying the following code:

u=Ttrain.Sex.unique()
a=[]
for i in range(len(u)):
    a.append(i)=u[i]
    print (a(i))

But I'm getting the following error:

SyntaxError: can't assign to function call

Please pardon my lack of knowledge because I've just started programming 2 weeks back. Any help/suggestion will be highly appreciated. Thank you.

WSMathias9
  • 669
  • 8
  • 15
IndigoChild
  • 842
  • 3
  • 11
  • 29

1 Answers1

1

when you call a.append(i), it is the argument, i, that gets appended to the list. So when you are assigning u[i] afterwards you are attempting to assign it to the result of the function call, but that is not something you can assign a value to. So when you append: it's the argument inside the parenthesis that is added to the list a.

u=Ttrain.Sex.unique()
a=[]
for i in range(len(u)):
    a.append(i)=u[i]
    print (a(i))

Also, when you print a(i), the syntax for indexing a list is a[i], since the call () operator will attempt to call the list, and since a list is just a container that makes no sense in this case.

Carl
  • 2,057
  • 1
  • 15
  • 20