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

Join by specific length of character

I want to join items in a list of lists based on these conditions:

  • Item 1 is exactly 6 characters of length, and
  • Item 2 is exactly 4 characters of length.

Input

list_of_lists = [
    ['912000', '0068', '6050457605142768217'],
    ['67','956532', '7201', '2549'],
    ['956811', '8334', '1748'],
    ['956718', '3693', '1551861252553'],
    ['956723', '0486', '86373']
]

Desired output:

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

list_of_lists = [['9120000068'], ['9565327201'], ['9568118334'], ['9567183693'], ['9567230486']]

>Solution :

You can just concatenate in a list comprehension, and use the len of the desired elements to filter out as you do so

>>> [[i+j] for i, j, *_ in list_of_lists if len(i)==6 and len(j)==4]
[['9120000068'], ['9568118334'], ['9567183693'], ['9567230486']]
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