0

I have the following code. How do i make it shorter? I just one range?

for x in range(len(list1)):
    if list1[x] == "AU":    
        d1[x] = d1[x]+" "+"not"
for y in range(len(list2)):
    if list2[y] == "AU":    
        d2[y] = d2[y]+" "+"not"
for z in range(len(list3)):
    if list3[z] == "AU":    
        d3[z] = d3[z]+" "+"not"
for a in range(len(list4)):
    if list4[a] == "AU":    
        d4[a] = d4[a]+" "+"not"
for b in range(len(list5)):
    if list5[b] == "AU":    
        d5[b] = d5[b]+" "+"not"

print(str(d1))
print(str(d2))
print(str(d3))
print(str(d4))
print(str(d5))
    

any help or any suggestion would be appreciating.. Thabk you

2
  • Use a nested loop (double loop) Commented Sep 22, 2022 at 21:16
  • what do you mean by "How do i make it shorter", please describe exactly what you want to achieve or the algorithm you want to implement so you can asymptotically optimize your program Commented Sep 24, 2022 at 6:48

1 Answer 1

1

Using nested loops

lists = [list1, list2, list3, list4, list5]
dlists = [d1, d2, d3, d4, d5]
for lst, d in zip(lists, dlists):
    for i in range(len(lst)):
        if lst[i] == 'AU':
            d[i] += " not"
    print(d)
Sign up to request clarification or add additional context in comments.

Comments

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.