Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Printing this type of list

I want to create a list like this:

mylist = ['a', 'ab', 'abc', 'abcd']

Is there any way to do this?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

def generate_list(start='a', end='d'):
    mylist = []
    start, end = ord(start), ord(end)
    for i in range(start, end + 1):
        sublist = [chr(j) for j in range(start, i + 1)]
        mylist.append(''.join(sublist))
    return mylist

mylist = generate_list()
print(mylist)

prints

['a', 'ab', 'abc', 'abcd']

another example:

mylist2 = generate_list(end='f')
print(mylist2)

prints

['a', 'ab', 'abc', 'abcd', 'abcde', 'abcdef']
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading