'Creating a function to concatenate strings based on len(array)

I am trying to concatenate a string to send a message via python>telegram My plan is so that the function is modular. It first import lines from a .txt file and based on that many lines it creates two different arrays array1[] and array2[], array1 will receive the values of the list as strings and array2 will receive user generated information to complemente what is stored in the same position as to a way to identify the differences in the array1[pos], as to put in a way:

while (k<len(list)):

array2[k]= str(input(array1[k]+": "))

k+=1

I wanted to create a single string to send in a single message like however in a way that all my list goes inside the same string

string1 = array1[pos]+": "+array2[pos]+"\n"

I have tried using while to compared the len but I kept recalling and rewriting my own string again and again.



Solution 1:[1]

It looks like what you're looking for is to have one list that comes directly from your text file. There's lots of ways to do that, but you most likely won't want to create a list iteratively with the index position. I would say to just append items to your list.

The accepted answer on this post has a good reference, which is basically the following:

import csv

with open('filename.csv', 'r') as fd:
    reader = csv.reader(fd)
    for row in reader:
        # do something

Which, in your case would mean something like this:

import csv

actual_text_list = []
with open('filename.csv', 'r') as fd:
    reader = csv.reader(fd)
    for row in reader:
        actual_text_list.append(row)

user_input_list = []
for actual_text in actual_text_list:
    the_users_input = input(f'What is your response to {actual_text}? ')    
    user_input_list.append(the_users_input)

This creates two lists, one with the actual text, and the other with the other's input. Which I think is what you're trying to do.


Another way, if the list in your text file will not have duplicates, you could consider using a dict, which is just a dictionary, a key-value data store. You would make the key the actual_text from the file, and the value the user_input. Another technique, you could make a list of lists.

import csv

actual_text_list = []
with open('filename.csv', 'r') as fd:
    reader = csv.reader(fd)
    for row in reader:
        actual_text_list.append(row)

dictionary = dict()
for actual_text in actual_text_list:
    the_users_input = input(f'What is your response to {actual_text}? ')    
    dictionary[actual_text] = the_users_input

Then you could use that data like this:

for actual_text, user_input in dictionary.items():
    print(f'In response to {actual_text}, you specified {user_input}.')

Solution 2:[2]

list_of_strings_from_txt = ["A","B","C"]
modified_list = [f"{w}: {input(f'{w}:')}" for w in list_of_strings_from_txt]

I guess? maybe?

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Joran Beasley