I've made an arabic to roman numerals converter in python all the way by myself (havent googled a single example)

```
def arabic_to_roman(num: int) -> str:
    roman_dict = {1000: 'M', 500: 'D', 100: 'C', 50: 'L', 10: 'X', 5: 'V', 1: 'I'}
    roman_num = ''
    exp_list = []
    chars = list(roman_dict.values())
    if num > 3999:
        return num

    else:
        for key in roman_dict.keys():
            exp = num // key
            num -= key * (num // key)
            exp_list.append(exp)

        pattern = [[chars[i], exp_list[i]] for i in range(len(chars))]
        print(pattern)

        # algorithm turning IIII into IV, VIIII into IX etc 

        for i in range(len(pattern)):
            for j in range(i - 2, i - 1):
                if pattern[j][1] > 3:
                    if pattern[j - 1][1] == 0:
                        pattern[j][1] = 1
                        pattern[j][0] = pattern[j][0] + pattern[j - 1][0]
                    else:
                        pattern[j][1] = 1
                        pattern[j - 1][1] = 0
                        pattern[j][0] = pattern[j][0] + pattern[j - 2][0]

        print(pattern)

        #concatenation the roman num from pattern
        for i in range(len(pattern)):
            roman_num += pattern[i][0][:2] * pattern[i][1]

        return roman_num

```

i dont like the parth where i use slicing in concatenation because my IIII -> IV algorithm creates extra characters in certain cases like XCD instead of XC or XCM instead of XC. How can i change the IIII -> IV part to make it work without using the slicing?

```
import random, time
start_time = time.time()

def my_test():

    my_list = [random.randint(1, 3999) for _ in range(1000000)]
    my_romanlist = [arabic_to_roman(i) for i in my_list]
    print(my_list[1], my_romanlist[1])

my_test()

print(f"{time.time() - start_time} seconds")

```

And here is my test, it takes about 16 seconds on my PC. is it good? how can i optimize the arabic\_to\_roman function to make it work faster?

Thanks in advance

okay now i've made a `roman_dict = {1000: 'M', 900: 'CM', 500: 'D', 400: 'CD', 100: 'C', 90: 'XC', 50: 'L', 40: 'XL', 10: 'X', 9: 'IX', 5: 'V', 4: 'IV', 1: 'I'}`

instead of older one and commented IIII-> IV part, but the test still takes approx the same amount of time. But the previous questions are still actual:)