0

I have multiple variables that were created, stringa - stringc. I am trying to find a way to iterative create these variable names and use them as input for a function, without having to type them in individually.

import string

stringa = ['item1', 'item2', item3']
stringb = ['sitem1', 'sitem2']
stringc = ['sitemc', 'sitemc']
    
def edittext(t):
    t = t.upper()
    return t

finallist = []
for i in range(3):
    letter = string.ascii_lowercase[i]
    finalist.append('string' + letter)
    finallist.append(edittext(('string' + letter).strip('\'')))

This is my output for the finallist.

Out [212]: ['stringa', 'STRINGA', 'stringb', 'STRINGB', 'stringc', 'STRINGC']

This is what I'm trying to accomplish:

['stringa', 'item1', 'item2', item3', 'stringb', 'sitem1', 'sitem2', 'stringc', 'sitemc', 'sitemc']

3
  • 2
    read about dictionaries in python Commented Jul 10, 2020 at 5:12
  • I thought about using dictionaries, but I don't think it would work. This is a simplified version of my problem. For the real problem, each of my variables contains a list of items. Commented Jul 10, 2020 at 5:18
  • 3
    the dictionary values CAN be lists Commented Jul 10, 2020 at 5:21

1 Answer 1

2

You can try with join and after mapping with list.map the string with your custom function:

import string

doc = ['This is the first string', 'This is the second string' ,'This is the third string']

def edittext(t):
    t = t.upper()
    return t

edited_doc=[]
for i,val in enumerate(doc):
    edited_doc.append('string'+string.ascii_lowercase[i])
    edited_doc.append(' '.join(map(edittext,val.split())))
    
print(edited_doc)

Output:

['stringa', 'THIS IS THE FIRST STRING', 'stringb', 'THIS IS THE SECOND STRING', 'stringc', 'THIS IS THE THIRD STRING']

Also as a suggestion, and as @Muhammadrasul said, you are assigning pairs of (key,value), so you can consider using a dictionary:

import string

doc = ['This is the first string', 'This is the second string' ,'This is the third string']

def edittext(t):
    t = t.upper()
    return t

edited_dict={'string'+string.ascii_lowercase[i]:' '.join(map(edittext,val.split())) for i,val in enumerate(doc)}

print(edited_dict)

Output:

{'stringa': 'THIS IS THE FIRST STRING', 'stringb': 'THIS IS THE SECOND STRING', 'stringc': 'THIS IS THE THIRD STRING'}
Sign up to request clarification or add additional context in comments.

9 Comments

Why use indexes? Python is a master of iteration without them. Please edit and iterate better on doc
I know that, I used indexes to the string.ascii_lowercase[i] assignation @Pynchia.
I will changed it to enumerate, don't worry. And I know the OP can just use upper, but maybe the function could be other, as you can see @Pynchia. Also, why don't you say from the beginning that I should use enumerate and why I should use it, instead of downvote the answer(I guess) and pointing as an "awful solution". That shouldn't be the attitude for this Q/A site. We are all learning here.
You're welcome @Lee, I'll edit the answer using enumerate. If you find it helpful, consider accepting it ! :)
|

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.