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']

Lee
  • 57
  • 7

1 Answers1

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'}
MrNobody33
  • 6,413
  • 7
  • 19
  • Why use indexes? Python is a master of iteration without them. Please edit and iterate better on `doc` – Pynchia Jul 10 '20 at 05:28
  • I know that, I used indexes to the `string.ascii_lowercase[i]` assignation @Pynchia. – MrNobody33 Jul 10 '20 at 05:30
  • I have edited the question a little bit. The reason I was trying to use index so was I can iteratively create variable names with a - z as suffixes. – Lee Jul 10 '20 at 05:31
  • Have you ever heard of `enumerate`? – Pynchia Jul 10 '20 at 05:33
  • 1
    Yes, that can be an option too. I didn't think about it. I will change it to enumerate based on this posts [https://stackoverflow.com/questions/4852944/what-is-faster-for-loop-using-enumerate-or-for-loop-using-xrange-in-python](https://stackoverflow.com/questions/4852944/what-is-faster-for-loop-using-enumerate-or-for-loop-using-xrange-in-python) and [https://stackoverflow.com/questions/522563/accessing-the-index-in-for-loops](https://stackoverflow.com/questions/522563/accessing-the-index-in-for-loops). – MrNobody33 Jul 10 '20 at 05:43
  • If you don't see why leave it as it is. It would not be a problem, just an awful solution. BTW: you do need an extra function. Actually, there's no need to raise words to uppercase, just convert the whole strings beforehand (note: using a generator expression would do it lazily, with no need to create another list. Make it pythonic) – Pynchia Jul 10 '20 at 05:43
  • This is very helpful! Thank you @MrNobody33 for the links! – Lee Jul 10 '20 at 05:52
  • 1
    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. – MrNobody33 Jul 10 '20 at 05:56
  • 1
    You're welcome @Lee, I'll edit the answer using enumerate. If you find it helpful, consider accepting it ! :) – MrNobody33 Jul 10 '20 at 05:58