1

To understand what I'm trying to do, first look at the code I've included below:

a = []
b = []
c = []

x = [a, b, c]

x[0] = [5, 10, 15]
x[1] = [30, 60, 90]
x[2] = [100, 200, 300]

Now, what I wish I could do is find a technique such that those last three lines would update the elements I had initially stored in x -- In other words, I wish that assigning a value to x[0] would directly update a, and assigning a value to x[1] would update b, and assigning a value to x[2] would update c. If this was the case, we'd be able to see the following outputs:

print(a)
>> [5, 10, 15]

print(b)
>> [30, 60, 90]

print(c)
>> [100, 200, 300]

I know this won't happen because assigning a value to x[i] just replaces the ith element in x, rather than "updating" the value of the ith element. And frankly, that makes much more sense and is obviously the way it should be. But, is there an alternate way to "update" variables in a list/array like this?

Here's a more detailed example of why I'm looking to do this:

dataset = 'directory\data.csv'

writer = []
director = []
runtime = []
language = []
country = []
genre = []
budget = []
year = []

features = [writer, director, runtime, language, country, genre, budget, year]

cnt = 0
for cnt in range(0,len(features)):
    with open(dataset,'r',encoding="latin1") as f:
        reader = csv.reader(f,delimiter=",",quotechar='"')
        for row in reader:
            features[cnt].append(row[cnt])
        features[cnt] = np.array(features[cnt])        # <<- trying to convert each element from list to numpy array

In other words, I'm trying to read data from a .csv file and copy each column to one of several different lists, which I initialized between dataset and features. Then, I need to convert these lists to numpy arrays so that I can use the np.where() function, since .index() won't do what I need for this task.

Ideally, after the most recent snippet of code, I'd be able to see all the elements in features had been updated. For example, the following outputs would ideally be what we see after running the above code:

print(country)
>> ['USA', 'USA', 'UK', 'Germany', 'USA', 'Italy', ... , 'Germany']

print(language)
>> ['English', 'English', 'English', 'German', 'English', 'Italian', ... ,'German']

# etc. etc.

Problem is, in my actual code, I have many more lists (ie. many more .csv columns) I need to copy, so that's why I put them all in one features list/array. I was hoping to be able to iteratively copy a different column into each list, and then convert each list to a numpy array, all in one compact loop. This would eliminate the need for a separate loop for each column/list I want to copy. But unfortunately, I can't figure out how to "update" each features element rather than write something entirely different in its place.

Is there a way to accomplish this? Apologies for poor syntax/efficiency anywhere, I'm not very experienced with Python.

1 Answer 1

2

You can do what you want. a, b, and c are mutable lists, and x stores references to the same lists referenced by a, b and c. Don't reassign elements of x, mutate them:

a = []
b = []
c = []

x = [a, b, c]
print(f'{x=}')
print(f'{a=}')
print(f'{b=}')
print(f'{c=}')

x[0][:] = [5, 10, 15]   # replace the entire content of the list at x[0]...
x[1][:] = [30, 60, 90]
x[2][:] = [100, 200, 300]
print(f'{x=}')
print(f'{a=}')
print(f'{b=}')
print(f'{c=}')
x=[[], [], []]
a=[]
b=[]
c=[]
x=[[5, 10, 15], [30, 60, 90], [100, 200, 300]]
a=[5, 10, 15]
b=[30, 60, 90]
c=[100, 200, 300]

x stores references to the lists in a, b, and c. x[0] is a is True, so if you mutate x[0] then a reflects the change. Same for the rest.

You could also x[0].append(item) or x[0].extend(list_of_items) to grow the a list.

But if you reassign x[0], then x[0] no longer references a. x[0] = [1,2,3] stores the reference to a new list in that location and no longer references the object a references.

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

1 Comment

Thank you so much, this is exactly what I was looking for. You are a godsend!

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.