Concatenation of strings in a list of dictionary based on matching pattern in python

Question:

I am trying to find a pattern ‘ABC’ in the values of dictionary of list. If the ‘ABC’ exist in the key value pair then it will concatenate the values of key ‘b’ and the rest of the key value pair will comes from the first dictionary till find the next ‘ABC’ in the dictionary ‘b’ of list of other elements

Sample Data:

L = [{'a': 0, 'b': 'radndaos43', 'c': 234},
     {'a': 1, 'b': 'lABChfgdd65a', 'c': 90011},
     {'a': 2, 'b': 'ddasda423sdABC', 'c': 324},
     {'a': 3, 'b': 'ddas236dasd', 'c': 123},
     {'a': 4, 'b': 'ddasd234asdABC', 'c': 900},
     {'a': 5, 'b': 'ddasd678hfasd', 'c': 342},
     {'a': 6, 'b': 'ddasd234dasdasdABC', 'c': 567}]

So, in the first element there is no ‘ABC’, so it should add the dictionary as it is and samething applies with the next one as there is ‘ABC’ but in the next element there is also ‘ABC’. 3rd and 4the element will concatenate and 5th and 6th will also concatenate.

Current Output:

[{'a': 1, 'b': 'lABChfgdd65a', 'c': 90011}, 
{'a': 2, 'b': 'ddasda423sdABC ddas236dasd','c': 324},
{'a': 4, 'b': 'ddasd234asdABC ddasd678hfasd', 'c': 900}]

Expected Output:

[{'a': 0, 'b': 'radndaos43', 'c': 234},
{'a': 1, 'b': 'lABChfgdd65a', 'c': 90011}, 
{'a': 2, 'b': 'ddasda423sdABC ddas236dasd','c': 324},
{'a': 4, 'b': 'ddasd234asdABC ddasd678hfasd', 'c': 900},
{'a': 6, 'b': 'ddasd234dasdasdABC', 'c': 567}]

1st and 6th element is missing from the output

This is what I tried so far,

updated_list = []
dict_holder = {}
for i in range(len(L)):
    if 'ABC' in L[i]['b'] and not dict_holder:
        dict_holder.update(L[i])
    elif 'ABC' not in L[i]['b'] and dict_holder:
        dict_holder.update({'b': dict_holder['b'] + ' ' + L[i]['b']})
    elif 'ABC' in L[i]['b'] and dict_holder:
        updated_list.append(dict_holder)
        dict_holder = {}
        dict_holder.update(L[i])

print(updated_list)
Asked By: GreekGod

||

Answers:

Here is how you would solve your problem:

for element in L: # don't need to use range() can access the element directly in the for loop as it is an iterable 
    if 'ABC' in element['b'] and not dict_holder:
        dict_holder.update(element)
    elif 'ABC' not in element['b'] and dict_holder:
        dict_holder.update({'b': dict_holder['b'] + ' ' + element['b']})
    elif 'ABC' in element['b'] and dict_holder:
        dict_holder = {} # changed this ordering as you were adding the previous dict_holer to updated_list then adding a new element 
        dict_holder.update(element)
        updated_list.append(dict_holder)
    else: # for 234 which has no ABC in it
        dict_holder.update(element)
        updated_list.append(element)

I included one code improvement to make it more readable (for loop iteration https://www.w3schools.com/python/python_for_loops.asp)

Answered By: Andrew Ryan
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.