Home > Back-end >  How to make the next 'for' loop continue loop from previous one through dictionary?
How to make the next 'for' loop continue loop from previous one through dictionary?

Time:10-17

If the input data is a list, it works properly

list_data = [7,10,9,20,2,5,15,6,25,2,8,17,23,9,24,45,3,22,6,20,4]
pos_1 = 0
pos_2 = 0
for i in range(len(list_data)):
    if list_data[i] >= 25:
        pos_1 = i
        break
print(f"Position1 which has value greater than equal to 25: {pos_1}")
for i in range(pos_1 1,len(list_data)):
    if list_data[i] >= 25:
        pos_2 = i
        break
print(f"Position2 which has value greater than equal to 25: {pos_2}")

The output (the 2nd for loop will not repeat the 1st for loop)

Position1 which has value greater than equal to 25: 8
Position2 which has value greater than equal to 25: 15

But if the data is a dictionary like this

dic_data = {1: 10, 3: 20, 6: 15, 8: 25, 11: 17, 12: 23, 14: 24, 15: 45, 17: 22, 19: 20}
index_1 = 0
for i in range(len(dic_data)):
    if(dic_data.values() >= 25):
        pos_1 = dic_data.keys()
        index_1 = i
        break
print(f"Position1 which has value greater than equal to 25: {pos_1}")

for i in range(index_1 1,len(dic_data)):
    if(dic_data.values() >= 25):
        pos_2 = dic_data.keys()
        break
print(f"Position2 which has value greater than equal to 25: {pos_2}")

Of course it must be an error

How to handle with the dictionary data? (Both of them aren't my actual code, it's just an example)

CodePudding user response:

You can use an iterator variable with the iter() function:

dic_data = {1: 10, 3: 20, 6: 15, 8: 25, 11: 17, 12: 23, 
            14: 24, 15: 45, 17: 22, 19: 20}

iData = iter(enumerate(dic_data.values()))

for i,value in iData:
    if (value >= 25):
        pos_1 = i
        break
print(f"Position1 which has value greater than equal to 25: {pos_1}")

for i,value in iData:
    if(value >= 25):
        pos_2 = i
        break
print(f"Position2 which has value greater than equal to 25: {pos_2}")

output:

Position1 which has value greater than equal to 25: 3
Position2 which has value greater than equal to 25: 7

Note that you cannot access a dictionary by a positional index, so the output has limited practical use

CodePudding user response:

The .keys() and .values() represents the whole list of keys or values, you can't do anything on it regarding comparison or index. You need to iterate on dict's pairs : items

dic_data = {1: 10, 3: 20, 6: 15, 8: 25, 11: 17, 12: 23, 14: 24, 15: 45, 17: 22, 19: 20}
index_1, pos_1, pos_2 = 0, 0, 0
for idx, (k, v) in enumerate(dic_data.items()):
    if v >= 25:
        pos_1 = k
        index_1 = idx
        break
print(f"Position1 which has value greater than equal to 25: {pos_1}")

for idx, (k, v) in enumerate(list(dic_data.items())[index_1   1:]):
    if v >= 25:
        pos_2 = k
        break
print(f"Position2 which has value greater than equal to 25: {pos_2}")
Position1 which has value greater than equal to 25: 8
Position2 which has value greater than equal to 25: 15
  • Related