'Join by specific length of character [closed]

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:

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


Solution 1:[1]

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']]

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1