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

Python create list from a other list

i want do edit a list in a python def function and get as return a new function. In the function the list should be sorted and some of the list-items should be replaced. The return should be a new list! Thx for help in advance..

i have a list1 = [a, d, b] the d should be replaced with a c and the list should be sorted. the new list should be [a, b, c]

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 :

Assuming that your letters start at the first letter and are in alphabetical order, this should work:

letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']

def Correct(l):
    l.sort()
    i = letters.index(l[0])
    return letters[i:min(len(letters), i+len(l))]

And to try it out:

test = ['a', 'd', 'b']
Correct(test)

Which gives:

['a', 'b', 'c']
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