0

So I am trying to store multiple variables in a list, each variable contains text. After that, I would like to perform some analysis on the text which is stored inside the variable that is stored in the list.

Code:

my_list = list[]

var_1 = 'I have lots of text'
var_2 = 'I have even more text'
var_3 = 'but wait there is more'

my_list.append('var_1','var_2','var_3')

for var in my_list:
    x = package.function('var')
    print x

what ends up happening is that the function is performed on the variable name not the text that is stored inside it. Thanks for your help in advance.

2
  • This is not valid python code. Please show us the actual code you're using Commented Oct 22, 2015 at 5:39
  • Use my_list = list() in your first line Commented Oct 22, 2015 at 5:40

3 Answers 3

1
var_1 = 'I have lots of text'
var_2 = 'I have even more text'
var_3 = 'but wait there is more'

my_list = [var_1, var_2, var_3]

for var in my_list:
    x = package.function(var)
    print x
Sign up to request clarification or add additional context in comments.

Comments

1

Your problem is that you are storing the string representation of your variable names:

my_list.append('var_1','var_2','var_3')

Furthermore, you cannot use the append like this. What you want to do is actually put in your variable names like this:

my_list = [var_1, var_2, var_3]

Finally, you are accessing your list index as 'var' in your loop:

for var in my_list:
    x = package.function('var')
    print(x)

to this:

for var in my_list:
    x = package.function(var)
    print(x)

Comments

1

As a new Python learning, i found some issue in your code.

Issue 1. This is how you declare a list in python.

my_list=list()

Issue 2. list.append method takes only 1 argument. So you can not pass all 3 variables at a same time.

 my_list = list()
 var_1 = 'I have lots of text'
 var_2 = 'I have even more text'
 var_3 = 'but wait there is more'

 #append all variables in list:
 my_list.append(var_1)
 my_list.append(var_2)
 my_list.append(var_3)

 #first way to print:
 for var in my_list:
    print var

 #second way to print:
 print my_list

 #third way to print:
 for idx,item in enumerate(my_list):
     print idx ,":", item

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.