How can i make these 3 lines of code more DRY

Abdullah Mallik

this code:

if len(group['elements']) > 0:
    groups.append(group)
    group = {'bla': '', 'elements': []}

Repeated 3 times in the example below. i want to make it in 1 line (atleast make it less). is it possible to do? then how can I do that?

collection_of_items = [
    ['strong', 'a', ['a'], '', 'strong', ['a'], ['a'], 'a', 'a', [], ''], 
    ['strong', 'a', ['a'], '', 'strong', 'a']
]

groups = []

for items in collection_of_items:
    group = {'bla': '', 'elements': []}
    for item in items:
        if hasattr(item, 'lower'):
            if item == 'strong':
                group['bla'] = item
            elif item =='a':
                group['elements'].append(item)
            elif item == '':
                # Make it DRY <---------------------------------------
                if len(group['elements']) > 0:
                    groups.append(group)
                    group = {'bla': '', 'elements': []}
        else:
            if 'a' in item:
                group['elements'].append(item[0])
            else:
                # Make it DRY <---------------------------------------
                if len(group['elements']) > 0:
                    groups.append(group)
                    group = {'bla': '', 'elements': []}

    # Make it DRY <---------------------------------------     
    if len(group['elements']) > 0:
                groups.append(group)
                group = {'bla': '', 'elements': []}

print(groups)

modify those 3 lines,

Note: Do anything but structure of the example code can't be changed

Sorry for mistakes.

ODiogoSilva

Put that code in a function, and call it whenever you want. But seriously, 4 space indent.

collection_of_items = [
  ['strong', 'a', ['a'], '', 'strong', ['a'], ['a'], 'a', 'a', [], ''], 
  ['strong', 'a', ['a'], '', 'strong', 'a']
]

groups = []

def my_func(g):
  if len(g['elements']) > 0:
    groups.append(g)
    g = {'bla': '', 'elements': []}
  return g

for items in collection_of_items:
  group = {'bla': '', 'elements': []}
  for item in items:
    if hasattr(item, 'lower'):
      if item == 'strong':
        group['bla'] = item
      elif item =='a':
        group['elements'].append(item)
      elif item == '':
        group = my_func(group)
    else:
      if 'a' in item:
        group['elements'].append(item[0])
      else:
        group = my_func(group)

  group = my_func(group)

print(groups)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How can I make my JavaScript code more DRY?

From Dev

How can I refactor these simple functions and make them more DRY?

From Dev

How can I refactor these simple functions and make them more DRY?

From Dev

How can I make this code more elegant?

From Dev

Can I make this method more generic/smaller? (DRY)

From Dev

How can I put this DRY code into a for loop?

From Dev

How can I make this code to find a pair with a sum more efficient?

From Dev

How can I refactor my kotlin code and make it more clear

From Dev

How can I refactor / make this code more pythonic?

From Dev

How could I could refactor a Pundit Policy to make it more DRY?

From Dev

How can I make a TextView automatically scroll as I add more lines of text?

From Dev

How can I use interfaces to allow for more than 1 struct type to make code more useable?

From Dev

ValueError: need more than 1 value to unpack, how can I make my code more robust?

From Dev

How do I make this code more responsive?

From Dev

How to make pundit policies more DRY?

From Dev

how i can put more lines in Line Plus Bar Chart using nvd3.js?

From Dev

how i can put more lines in Line Plus Bar Chart using nvd3.js?

From Dev

How can I make this PHP lines shorter?

From Dev

How can I DRY out this F# code? (Fluent Interface)

From Dev

How can i keep my code dry in ajax requests?

From Dev

How can I make my D3 code cleaner

From Dev

How can I make this Observable more reusable?

From Dev

How can I make this loop more efficient?

From Dev

How can I make this method more concise?

From Dev

How can I make this Makefile more generic?

From Dev

How can I make the button more visible?

From Dev

How can I make this loop more efficient?

From Dev

How can I make a Read More Button?

From Dev

How can i refactor these lines of code?

Related Related

  1. 1

    How can I make my JavaScript code more DRY?

  2. 2

    How can I refactor these simple functions and make them more DRY?

  3. 3

    How can I refactor these simple functions and make them more DRY?

  4. 4

    How can I make this code more elegant?

  5. 5

    Can I make this method more generic/smaller? (DRY)

  6. 6

    How can I put this DRY code into a for loop?

  7. 7

    How can I make this code to find a pair with a sum more efficient?

  8. 8

    How can I refactor my kotlin code and make it more clear

  9. 9

    How can I refactor / make this code more pythonic?

  10. 10

    How could I could refactor a Pundit Policy to make it more DRY?

  11. 11

    How can I make a TextView automatically scroll as I add more lines of text?

  12. 12

    How can I use interfaces to allow for more than 1 struct type to make code more useable?

  13. 13

    ValueError: need more than 1 value to unpack, how can I make my code more robust?

  14. 14

    How do I make this code more responsive?

  15. 15

    How to make pundit policies more DRY?

  16. 16

    how i can put more lines in Line Plus Bar Chart using nvd3.js?

  17. 17

    how i can put more lines in Line Plus Bar Chart using nvd3.js?

  18. 18

    How can I make this PHP lines shorter?

  19. 19

    How can I DRY out this F# code? (Fluent Interface)

  20. 20

    How can i keep my code dry in ajax requests?

  21. 21

    How can I make my D3 code cleaner

  22. 22

    How can I make this Observable more reusable?

  23. 23

    How can I make this loop more efficient?

  24. 24

    How can I make this method more concise?

  25. 25

    How can I make this Makefile more generic?

  26. 26

    How can I make the button more visible?

  27. 27

    How can I make this loop more efficient?

  28. 28

    How can I make a Read More Button?

  29. 29

    How can i refactor these lines of code?

HotTag

Archive