0

I was wondering whether its possible to get a variable (basically a list) from one python file into the main program file and then pass it into a class object in the main program file without first defining the variable (WORDS below) in the program file. I will explain the situation below. File words.py has a list named wordlist. I import this wordlist into the main program file main.py like:

# words.py file
wordlist = ['an',''..] # some list of words

# main.py
from words import wordlist

WORDS = wordlist.copy()
WORDS.remove('an')

mc = models_class(word_list = WORDS)

As i have tried the above works if i first define a local variable WORDS but can it work if i directly pass the wordlist into the class object and apply remove there, like below.

mc = models_class(word_list = wordlist.remove('an'))

1 Answer 1

2

You can not do like word_list = wordlist.remove('an') because remove is an in-place operation which just update the original list and return None.

See python more on list

You might have noticed that methods like insert, remove or sort that only modify the list have no return value printed – they return the default None. 1 This is a design principle for all mutable data structures in Python.

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

1 Comment

Thanks for the explanation. Yes, you are right i did notice the None being returned if i try to print and see the output of remove. I just didn't knew that the reason for that was because these are in-place operations as you explained. Thanks!!

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.