Getting error as TypeError: 'function' object is not subscriptable
Question:
I have a list in a separate document that resides in a function raw_data()
this list is then called by a function in my document data_set()
.
I am trying to access the list and then index certain items in the following way:
#import function for data_source
from data_source import raw_data
def data_set():
....
return raw_data() # return the random data set
#function to index certain parts of my list
def data_drawing_loop(data_set):
#variables for following loop
rand_data = data_set
data_heading = data_set[0][2] # error point
for data_heading in rand_data:
...
#my function is then nested within the following function
def visualise_data(data_set):
data_drawing_loop(data_set)
....
#and is finally called via
visualise_data(data_set)
I have tried replacing def data_drawing_loop(data_set)
with (raw_data)
, defining the variable at the beginning of my code etc.
I’m still not sure what I’m doing wrong, and I keep getting the same issue.
Answers:
It seems like the issue is with how you’re accessing the list within the data_set function. In particular, when you call rand_data = data_set, you’re not actually accessing the list within data_set, but rather assigning rand_data to be a reference to the same list object as data_set.
Then, when you try to access data_heading = data_set[0][2], you’re actually trying to access the third element of the first element of the data_set function object itself (which is not a list, but rather a function). This is likely causing the error.
To fix this, you should modify your data_drawing_loop function to access the list returned by the data_set function directly, like so:
def data_drawing_loop(data_set):
rand_data = data_set()
data_heading = rand_data[0][2]
heading_tru_fal = False
heading_as_a_number = 0
for data_heading in rand_data:
...
Here, data_set() calls the data_set function and returns the list, which is then assigned to rand_data. You can then access elements of rand_data using standard list indexing.
I have a list in a separate document that resides in a function raw_data()
this list is then called by a function in my document data_set()
.
I am trying to access the list and then index certain items in the following way:
#import function for data_source
from data_source import raw_data
def data_set():
....
return raw_data() # return the random data set
#function to index certain parts of my list
def data_drawing_loop(data_set):
#variables for following loop
rand_data = data_set
data_heading = data_set[0][2] # error point
for data_heading in rand_data:
...
#my function is then nested within the following function
def visualise_data(data_set):
data_drawing_loop(data_set)
....
#and is finally called via
visualise_data(data_set)
I have tried replacing def data_drawing_loop(data_set)
with (raw_data)
, defining the variable at the beginning of my code etc.
I’m still not sure what I’m doing wrong, and I keep getting the same issue.
It seems like the issue is with how you’re accessing the list within the data_set function. In particular, when you call rand_data = data_set, you’re not actually accessing the list within data_set, but rather assigning rand_data to be a reference to the same list object as data_set.
Then, when you try to access data_heading = data_set[0][2], you’re actually trying to access the third element of the first element of the data_set function object itself (which is not a list, but rather a function). This is likely causing the error.
To fix this, you should modify your data_drawing_loop function to access the list returned by the data_set function directly, like so:
def data_drawing_loop(data_set):
rand_data = data_set()
data_heading = rand_data[0][2]
heading_tru_fal = False
heading_as_a_number = 0
for data_heading in rand_data:
...
Here, data_set() calls the data_set function and returns the list, which is then assigned to rand_data. You can then access elements of rand_data using standard list indexing.