0

I am looping through a JSON dict to capture some data, I receive my expectant output, but too many times, where should either loop break to correctly capture the output? This out shows the formatted string more times than expected?

for dSr in lServiceRequests:
            dListOfLa311ElectronicWaste = dSr["ListOfLa311ElectronicWaste"]
            if("La311ElectronicWaste" in dListOfLa311ElectronicWaste):
                lLa311ElectronicWaste = dListOfLa311ElectronicWaste["La311ElectronicWaste"]
                for dla311ElectronicWaste in lLa311ElectronicWaste:
                     v_ewaste_item_count= dla311ElectronicWaste[K_ewaste_Item_Count]
            dListOfLa311ElectronicWaste = dSr["ListOfLa311ElectronicWaste"]
            if("La311ElectronicWaste" in dListOfLa311ElectronicWaste):
                lLa311ElectronicWaste = dListOfLa311ElectronicWaste["La311ElectronicWaste"]
                for dla311ElectronicWaste in lLa311ElectronicWaste:
                    v_ewaste_type = dla311ElectronicWaste[K_ewaste_type]
                v_ewaste_item_info += '{0},  {1} '.format(v_ewaste_item_count, v_ewaste_type)
                print v_ewaste_item_info

Output:

1,  Cell Phone 
 1,  Cell Phone 1,  Computers 
 1,  Cell Phone 1,  Computers 2,  Cell Phone 
 1,  Cell Phone 1,  Computers 2,  Cell Phone 2,  Cell Phone 
 1,  Cell Phone 1,  Computers 2,  Cell Phone 2,  Cell Phone 1,  Laptops/Tablets 
 1,  Cell Phone 1,  Computers 2,  Cell Phone 2,  Cell Phone 1,  Laptops/Tablets 1,  Cell Phone 
 1,  Cell Phone 1,  Computers 2,  Cell Phone 2,  Cell Phone 1,  Laptops/Tablets 1,  Cell Phone 2,  TV (Any Size) 
 1,  Cell Phone 1,  Computers 2,  Cell Phone 2,  Cell Phone 1,  Laptops/Tablets 1,  Cell Phone 2,  TV (Any Size) 1,  Cell Phone 
 1,  Cell Phone 1,  Computers 2,  Cell Phone 2,  Cell Phone 1,  Laptops/Tablets 1,  Cell Phone 2,  TV (Any Size) 1,  Cell Phone 2,  Cell Phone 
 1,  Cell Phone 1,  Computers 2,  Cell Phone 2,  Cell Phone 1,  Laptops/Tablets 1,  Cell Phone 2,  TV (Any Size) 1,  Cell Phone 2,  Cell Phone 1,  Cell Phone 
 1,  Cell Phone 1,  Computers 2,  Cell Phone 2,  Cell Phone 1,  Laptops/Tablets 1,  Cell Phone 2,  TV (Any Size) 1,  Cell Phone 2,  Cell Phone 1,  Cell Phone 1,  Copier/Scanner 
 1,  Cell Phone 1,  Computers 2,  Cell Phone 2,  Cell Phone 1,  Laptops/Tablets 1,  Cell Phone 2,  TV (Any Size) 1,  Cell Phone 2,  Cell Phone 1,  Cell Phone 1,  Copier/Scanner 4,  Laptops/Tablets 
 1,  Cell Phone 1,  Computers 2,  Cell Phone 2,  Cell Phone 1,  Laptops/Tablets 1,  Cell Phone 2,  TV (Any Size) 1,  Cell Phone 2,  Cell Phone 1,  Cell Phone 1,  Copier/Scanner 4,  Laptops/Tablets 4,  Stereo/Speakers 
 1,  Cell Phone 
 1,  Cell Phone 1,  Computers 
 1,  Cell Phone 1,  Computers 2,  Cell Phone 
 1,  Cell Phone 1,  Computers 2,  Cell Phone 2,  Cell Phone 
 1,  Cell Phone 1,  Computers 2,  Cell Phone 2,  Cell Phone 1,  Laptops/Tablets 
 1,  Cell Phone 1,  Computers 2,  Cell Phone 2,  Cell Phone 1,  Laptops/Tablets 1,  Cell Phone 
 1,  Cell Phone 1,  Computers 2,  Cell Phone 2,  Cell Phone 1,  Laptops/Tablets 1,  Cell Phone 2,  TV (Any Size) 
 1,  Cell Phone 1,  Computers 2,  Cell Phone 2,  Cell Phone 1,  Laptops/Tablets 1,  Cell Phone 2,  TV (Any Size) 1,  Cell Phone 
 1,  Cell Phone 1,  Computers 2,  Cell Phone 2,  Cell Phone 1,  Laptops/Tablets 1,  Cell Phone 2,  TV (Any Size) 1,  Cell Phone 2,  Cell Phone 
 1,  Cell Phone 1,  Computers 2,  Cell Phone 2,  Cell Phone 1,  Laptops/Tablets 1,  Cell Phone 2,  TV (Any Size) 1,  Cell Phone 2,  Cell Phone 1,  Cell Phone 
 1,  Cell Phone 1,  Computers 2,  Cell Phone 2,  Cell Phone 1,  Laptops/Tablets 1,  Cell Phone 2,  TV (Any Size) 1,  Cell Phone 2,  Cell Phone 1,  Cell Phone 1,  Copier/Scanner 
 1,  Cell Phone 1,  Computers 2,  Cell Phone 2,  Cell Phone 1,  Laptops/Tablets 1,  Cell Phone 2,  TV (Any Size) 1,  Cell Phone 2,  Cell Phone 1,  Cell Phone 1,  Copier/Scanner 4,  Laptops/Tablets 
 1,  Cell Phone 1,  Computers 2,  Cell Phone 2,  Cell Phone 1,  Laptops/Tablets 1,  Cell Phone 2,  TV (Any Size) 1,  Cell Phone 2,  Cell Phone 1,  Cell Phone 1,  Copier/Scanner 4,  Laptops/Tablets 4,  Stereo/Speakers 
 1,  Cell Phone 
 1,  Cell Phone 1,  Computers 
 1,  Cell Phone 1,  Computers 2,  Cell Phone 
 1,  Cell Phone 1,  Computers 2,  Cell Phone 2,  Cell Phone 
 1,  Cell Phone 1,  Computers 2,  Cell Phone 2,  Cell Phone 1,  Laptops/Tablets 
 1,  Cell Phone 1,  Computers 2,  Cell Phone 2,  Cell Phone 1,  Laptops/Tablets 1,  Cell Phone 
 1,  Cell Phone 1,  Computers 2,  Cell Phone 2,  Cell Phone 1,  Laptops/Tablets 1,  Cell Phone 2,  TV (Any Size) 
 1,  Cell Phone 1,  Computers 2,  Cell Phone 2,  Cell Phone 1,  Laptops/Tablets 1,  Cell Phone 2,  TV (Any Size) 1,  Cell Phone 
 1,  Cell Phone 1,  Computers 2,  Cell Phone 2,  Cell Phone 1,  Laptops/Tablets 1,  Cell Phone 2,  TV (Any Size) 1,  Cell Phone 2,  Cell Phone 
 1,  Cell Phone 1,  Computers 2,  Cell Phone 2,  Cell Phone 1,  Laptops/Tablets 1,  Cell Phone 2,  TV (Any Size) 1,  Cell Phone 2,  Cell Phone 1,  Cell Phone 
 1,  Cell Phone 1,  Computers 2,  Cell Phone 2,  Cell Phone 1,  Laptops/Tablets 1,  Cell Phone 2,  TV (Any Size) 1,  Cell Phone 2,  Cell Phone 1,  Cell Phone 1,  Copier/Scanner 
 1,  Cell Phone 1,  Computers 2,  Cell Phone 2,  Cell Phone 1,  Laptops/Tablets 1,  Cell Phone 2,  TV (Any Size) 1,  Cell Phone 2,  Cell Phone 1,  Cell Phone 1,  Copier/Scanner 4,  Laptops/Tablets 
 1,  Cell Phone 1,  Computers 2,  Cell Phone 2,  Cell Phone 1,  Laptops/Tablets 1,  Cell Phone 2,  TV (Any Size) 1,  Cell Phone 2,  Cell Phone 1,  Cell Phone 1,  Copier/Scanner 4,  Laptops/Tablets 4,  Stereo/Speakers 
 1,  Cell Phone 
 1,  Cell Phone 1,  Computers 
 1,  Cell Phone 1,  Computers 2,  Cell Phone 
 1,  Cell Phone 1,  Computers 2,  Cell Phone 2,  Cell Phone 
 1,  Cell Phone 1,  Computers 2,  Cell Phone 2,  Cell Phone 1,  Laptops/Tablets 
 1,  Cell Phone 1,  Computers 2,  Cell Phone 2,  Cell Phone 1,  Laptops/Tablets 1,  Cell Phone 
 1,  Cell Phone 1,  Computers 2,  Cell Phone 2,  Cell Phone 1,  Laptops/Tablets 1,  Cell Phone 2,  TV (Any Size) 
 1,  Cell Phone 1,  Computers 2,  Cell Phone 2,  Cell Phone 1,  Laptops/Tablets 1,  Cell Phone 2,  TV (Any Size) 1,  Cell Phone 
 1,  Cell Phone 1,  Computers 2,  Cell Phone 2,  Cell Phone 1,  Laptops/Tablets 1,  Cell Phone 2,  TV (Any Size) 1,  Cell Phone 2,  Cell Phone 
 1,  Cell Phone 1,  Computers 2,  Cell Phone 2,  Cell Phone 1,  Laptops/Tablets 1,  Cell Phone 2,  TV (Any Size) 1,  Cell Phone 2,  Cell Phone 1,  Cell Phone 
 1,  Cell Phone 1,  Computers 2,  Cell Phone 2,  Cell Phone 1,  Laptops/Tablets 1,  Cell Phone 2,  TV (Any Size) 1,  Cell Phone 2,  Cell Phone 1,  Cell Phone 1,  Copier/Scanner 
 1,  Cell Phone 1,  Computers 2,  Cell Phone 2,  Cell Phone 1,  Laptops/Tablets 1,  Cell Phone 2,  TV (Any Size) 1,  Cell Phone 2,  Cell Phone 1,  Cell Phone 1,  Copier/Scanner 4,  Laptops/Tablets 
 1,  Cell Phone 1,  Computers 2,  Cell Phone 2,  Cell Phone 1,  Laptops/Tablets 1,  Cell Phone 2,  TV (Any Size) 1,  Cell Phone 2,  Cell Phone 1,  Cell Phone 1,  Copier/Scanner 4,  Laptops/Tablets 4,  Stereo/Speakers 
 1,  Cell Phone 
 1,  Cell Phone 1,  Computers 
 1,  Cell Phone 1,  Computers 2,  Cell Phone 
 1,  Cell Phone 1,  Computers 2,  Cell Phone 2,  Cell Phone 
 1,  Cell Phone 1,  Computers 2,  Cell Phone 2,  Cell Phone 1,  Laptops/Tablets 
 1,  Cell Phone 1,  Computers 2,  Cell Phone 2,  Cell Phone 1,  Laptops/Tablets 1,  Cell Phone 
 1,  Cell Phone 1,  Computers 2,  Cell Phone 2,  Cell Phone 1,  Laptops/Tablets 1,  Cell Phone 2,  TV (Any Size) 
 1,  Cell Phone 1,  Computers 2,  Cell Phone 2,  Cell Phone 1,  Laptops/Tablets 1,  Cell Phone 2,  TV (Any Size) 1,  Cell Phone 
 1,  Cell Phone 1,  Computers 2,  Cell Phone 2,  Cell Phone 1,  Laptops/Tablets 1,  Cell Phone 2,  TV (Any Size) 1,  Cell Phone 2,  Cell Phone 
 1,  Cell Phone 1,  Computers 2,  Cell Phone 2,  Cell Phone 1,  Laptops/Tablets 1,  Cell Phone 2,  TV (Any Size) 1,  Cell Phone 2,  Cell Phone 1,  Cell Phone 
 1,  Cell Phone 1,  Computers 2,  Cell Phone 2,  Cell Phone 1,  Laptops/Tablets 1,  Cell Phone 2,  TV (Any Size) 1,  Cell Phone 2,  Cell Phone 1,  Cell Phone 1,  Copier/Scanner 
 1,  Cell Phone 1,  Computers 2,  Cell Phone 2,  Cell Phone 1,  Laptops/Tablets 1,  Cell Phone 2,  TV (Any Size) 1,  Cell Phone 2,  Cell Phone 1,  Cell Phone 1,  Copier/Scanner 4,  Laptops/Tablets 
 1,  Cell Phone 1,  Computers 2,  Cell Phone 2,  Cell Phone 1,  Laptops/Tablets 1,  Cell Phone 2,  TV (Any Size) 1,  Cell Phone 2,  Cell Phone 1,  Cell Phone 1,  Copier/Scanner 4,  Laptops/Tablets 4,  Stereo/Speakers 
2
  • 1
    at least you need to unify your own style to name a variable... Commented Jun 15, 2015 at 18:06
  • Can you explain to us what's your aim and what's the JSON input? Commented Jun 15, 2015 at 18:07

4 Answers 4

4

You have options when you need to break out of more than one loop.

1 - Put this code in a separate function and use a return.

2 - use a flag variable to know when to exit the outer loop after breaking from the first

3 - use an exception to exit

And I'm sure there are even more ways to do this!

Sign up to request clarification or add additional context in comments.

Comments

1

You could always put it in a try and create an exception and catch it to get out.

class BreakAllLoops(Exception):
    print("Breaking loops")

try:
    while True:
        for i in range(10):
            print(i)
            if(i > 5):
                raise BreakAllLoops

except BreakAllLoops:
    print("Left loops")

Comments

1

You don't need to break anything. Instead of print v_ewaste_item_info do

L=[]
for dSr in lServiceRequests:
...
if v_ewaste_item_info not in L:
    L.append(v_ewaste_item_info)

Then print L. It will contain no duplicates.

Comments

0

Considering the following lists as an example / analogy:

l1 = [x for x in range(10)]
l2 = [x for x in range(20)]

Instead of iterating through them like this:

for i in l1:
    for j in l2:
        print(i)
        print(j)

Iterate through them like this:

for i in l1, l2:
    print(i)

Try to apply it to your code (Using commas and just one loop, and accessing the elements accordingly).

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.