1

Hello I have a list in which elemnets are in pair of 3 list given below,

labels = ['', '', '5000','', '2', '','', '', '1000','mm-dd-yy', '', '','', '', '15','dd/mm/yy', '', '', '', '3', '','', '', '200','', '2', '','mm-dd-yy', '', '','', '', '','', '', '']

in above list elements are coming in pair of 3 i.e. ('', '', '5000') one pair, ('', '2', '') second pair, ('mm-dd-yy', '', '') third pair and so on.

now i want to check ever 3 pairs in list and get the element which is not blank.

('', '', '5000') gives '5000'

('', '2', '') gives '2'

('mm-dd-yy', '', '') gives 'mm-dd-yy'

and if all three are blank it should return blank i.e.

('', '', '') gives '' like last 2 pair in list

so from the above list my output should be:

required_list = ['5000','2','1000','mm-dd-yy','15','dd/mm/yy','3','200','2','mm-dd-yy','','']
Mohit
  • 127
  • 2
  • 8
  • Does this answer your question? [How to iterate over a list in chunks](https://stackoverflow.com/questions/434287/how-to-iterate-over-a-list-in-chunks) – Abdul Aziz Barkat Jan 24 '23 at 04:10
  • @AbdulAzizBarkat thanks but not exactly above is dividing the list in chunks, it is helpful, but to get required result, need get in list format. – Mohit Jan 24 '23 at 04:20
  • What is the exact problem you are stuck on? Currently you seem to have just given an algorithm... – Abdul Aziz Barkat Jan 24 '23 at 04:22
  • What is your specific question? "How do I fetch items three at a time from a list?" or "How do I identify at most one nonblank value from a sequence of three values?" – John Gordon Jan 24 '23 at 04:30

4 Answers4

2

as it is fixed you have to create 3 pairs each time you can do with for loop by specifying step in range(start,end,step)

labels = ['', '', '5000','', '2', '','', '', '1000','mm-dd-yy', '', '','', '', '15','dd/mm/yy', '', '', '', '3', '','', '', '200','', '2', '','mm-dd-yy', '', '','', '', '','', '', '']
res1=[]
for i in range(0,len(labels),3):
    res1.append(labels[i]+labels[i+1]+labels[i+2])
print(res1)

#List Comprehension
res2=[labels[i]+labels[i+1]+labels[i+2] for i in range(0,len(labels),3)]

print(res2)

Output:

['5000', '2', '1000', 'mm-dd-yy', '15', 'dd/mm/yy', '3', '200', '2', 'mm-dd-yy', '', '']
Yash Mehta
  • 2,025
  • 3
  • 9
  • 20
0

Iterate over a 3 sliced list and then get the first non-null element with next.

labels = ['', '', '5000','', '2', '','', '', '1000','mm-dd-yy', '', '','', '', '15','dd/mm/yy', '', '', '', '3', '','', '', '200','', '2', '','mm-dd-yy', '', '','', '', '','', '', '']
length = len(labels)
list_by_3 = [labels[i:i+3] for i in range(0, length, 3)]

required_list = []
for triplet in list_by_3:
  required_list.append(
    next(i for i in triplet if i, "")
  )
>>> required_list
['5000', '2', '1000', 'mm-dd-yy', '15', 'dd/mm/yy', '3', '200', '2', 'mm-dd-yy', '', '']
scr
  • 853
  • 1
  • 2
  • 14
  • 1
    Note that `next` accepts an optional argument, a default value to be returned if the iterator is empty. You can replace your `if / else` with `next((i for i in triplet if i), '')`. [Documentation for `next`](https://docs.python.org/3/library/functions.html#next). – Stef Jan 24 '23 at 09:29
0

I think this should give you the required result. Not ideal performance but gets the job done and should be pretty easy to follow

    labels = ['', '', '5000','', '2', '','', '', '1000','mm-dd-yy', '', '','', '', '15','dd/mm/yy', '', '', '', '3', '','', '', '200','', '2', '','mm-dd-yy', '', '','', '', '','', '', '']

    def chunks(ls):
        chunks = []

        start = 0
        end = len(ls)
        step = 3
        for i in range(start, end, step):
            chunks.append(ls[i:i+step])

        return chunks

    output = []
    for chunk in chunks(labels):
        nonEmptyItems = [s for s in chunk if len(s) > 0]
        if len(nonEmptyItems) > 0:
            output.append(nonEmptyItems[0])
        else:
            output.append('')

    print(output)
Curtis Cali
  • 123
  • 7
0

All the previous answers laboriously create a new list of triplets, then iterate on that list of triplets.

There is no need to create this intermediate list.

def gen_nonempty_in_triplets(labels):
    return [max(labels[i:i+3], key=len) for i in range(0, len(labels), 3)]

labels = ['', '', '5000','', '2', '','', '', '1000','mm-dd-yy', '', '','', '', '15','dd/mm/yy', '', '', '', '3', '','', '', '200','', '2', '','mm-dd-yy', '', '','', '', '','', '', '']
print(gen_nonempty_in_triplets(labels))
# ['5000', '2', '1000', 'mm-dd-yy', '15', 'dd/mm/yy', '3', '200', '2', 'mm-dd-yy', '', '']

Interestingly, there are many different ways to implement "get the element which is not blank".

I chose to use max(..., key=len) to select the longest string.

Almost every answer you received uses a different method!

Here are a few different methods that were suggested. They are equivalent when at most one element of the triplet is nonempty, but they behave differently if the triplet contains two or more nonempty elements.

# selects the longest string
max(labels[i:i+3], key=len)

# selects the first nonempty string
next((i for i in labels[i:i+3] if i), '')

# concatenates all three strings
labels[i]+labels[i+1]+labels[i+2]
Stef
  • 13,242
  • 2
  • 17
  • 28