1

I'm trying to write a program where I have two lists & one dictionary:

dict = {'fruit1' : 'apple', 'fruit2' :'banana', 'fruit3':'cherry' ....and so on} 
list1 = ['a','b','c','d','e'....]
list2 = ['fruit1', 'fruit2','fruit3'....]

I have a program which looks like this. [This is not right at all, but it helps represent what I'm trying to get as the result].

for obj1 in list1:
    for obj_2 in list2:
        print(obj1)
        print(obj_2)
        print(dict[obj_2])

My need is to loop this in a way where obj_2 changes every nth loop, but obj_1 changes every loop. How can I achieve this? So my result would look like(considering nth loop is 3rd loop):

a
fruit1
apple
b
fruit1
apple
c
fruit1
apple
d
fruit2
banana
e
fruit2
banana
f
fruit2
banana
g
fruit3
cherry
.
.
.

2 Answers 2

1

Use a counter variable instead of nested loops. Increment the counter each time through the loop, and when it reaches n wrap it back to 0 and increment the index into list2.

n = 3
list2_index = 0
counter = 0
for obj1 in list1:
    obj_2 = list2[list2_index]
    print(obj1)
    print(obj_2)
    print(dict[obj_2])
    counter += 1
    if counter == n:
        counter = 0
        list2_index += 1

BTW, don't use dict as a variable name, it's the name of a built-in type.

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

Comments

1

so all you have to do to achive is change the place of your two for loops.

#BTW it isn't adviced to use reserved keywords for variable names so dont use Dict for a variable name
myDict = {'fruit1' : 'apple', 'fruit2' :'banana', 'fruit3':'cherry'} 
list1 = ['a','b','c','d','e']
list2 = ['fruit1', 'fruit2','fruit3']

#so in this nested loop obj2 only changs after  the n loops (n being the length of list1)
#which is after list1 is complete and it does that over and over 
#until list2 is complete
for obj2 in list2:
    for obj1 in list1:
        print(obj1)
        print(obj2)
        print(myDict[obj2])

EDIT I might have misunderstood what you meant by third loop as @Barmar suggested. so if that is what you meant here is another piece of code.

myDict = {'fruit1' : 'apple', 'fruit2' :'banana', 'fruit3':'cherry'} 
list1 = ['a','b','c','d','e']
list2 = ['fruit1', 'fruit2','fruit3']

#a variable to keep track of the nth loop
nthLoop = 1 
for obj2 in list2:
    for obj1 in list1:
        #if you print for three times which is what you wanted for your nthloop to be 
        #then break, which will break out of this nested loop allowing to only print 3 times and also set the 
        #nthLoop back to zero so that it will work nicely for the next iteration
        if nthLoop > 3:
            nthLoop = 0
            break
        print(obj1)
        print(obj2)
        print(myDict[obj2])
        nthLoop += 1

3 Comments

This prints fruit1 apple 5 times, not 3 times.
Thanks for your answer! However, I'm trying to limit the print of "fruit1" to 3 times only, not based on the length of the list1 If we assume the list1 is 26 characters long
@user14131697 How about now? does the new solution answer your question

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.