growing list of strings using the string input len as the index to place the string

Viewed 33

I am trying to get a growing list of strings, with each subsequent string input, place that string in the index equal to its length and then return the new combined string (as a string) after n amount of inputs. Here is what I have far...

# user input number of elements
user_input_count = int(input("Enter number of elements: "))

# repeat counter
repeat_counter = 1

while repeat_counter < user_input_count:
    user_input_string = input(f"Enter the value of input #{repeat_counter}: ")
    split_string_list = list(user_input_string)
    print(f"output: {split_string_list}")
    print(repeat_counter)
    while repeat_counter < user_input_count:
        repeat_counter += 1
        new_input_string = input(f"Enter the value of input #{repeat_counter}: ")
        x = list(new_input_string)
        split_string_list.insert(len(new_input_string), x)
        print(split_string_list)```
2 Answers

This code is storing your inputs in a 2d-list. The first dimension is the length of the strings. The second dimension is a list of strings that belong to the length.

The list looks like this:

split_string_list = [[],["a","b],["ab","ac"],["abc"]] #etc.

The code:

# user input number of element

user_input_count = int(input("Enter number of elements: "))

# repeat counter
repeat_counter = 1
split_string_list = []
concatenated_string = ""

for counter in range(0, user_input_count):
    user_input_string = input(f"Enter the value of input #{repeat_counter}: ")
    concatenated_string += user_input_string
    list_length = len(split_string_list)
    # this if you want to append the concatenated string to the list
    # string_length = len(concatenated_string)
    # else
    string_length = len(user_input_string)

    if string_length > list_length:
        # make the list longer so that it can fit the new string at index of length
        split_string_list += ([] for x in range(string_length - list_length + 1))

    # append it at the index of the length of the string
    split_string_list[string_length].append(user_input_string)
    # this line if you wish to append the concatenated string instead
    # split_string_list[len(concatenated_string) ].append(concatenated_string)

    print(split_string_list)

    # call the last element (the newest element within a subarray) of the list
    print(f"output: {split_string_list[string_length][-1]}")
    print(counter)

So I was able to get the results I wanted with the code snippet below...

# user input number of elements
user_input_count = int(input("Enter number of elements: "))

# repeat counter
repeat_counter = 1

while repeat_counter < user_input_count:
    user_input_string = input(f"Enter the value of input #{repeat_counter}: ")
    split_string_list = list(user_input_string)
    print(f"output: {split_string_list}")
    print(repeat_counter)
    while repeat_counter < user_input_count:
        repeat_counter += 1
        new_input_string = input(f"Enter the value of input #{repeat_counter}: ")
        x = list(new_input_string)
        split_string_list.insert(len(new_input_string), x)
        flat_list = [item for sublist in split_string_list for item in sublist]
        print(*flat_list, sep="")

I will be totally honest, the code doesn't seem that great meaning that it could probably be optimized and shortened by a lot. One part that seems redundant is the nested while loop.

Related