Collectives™ on Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most.
Learn more about Collectives
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Learn more about Teams
list1=[1,2,3,4,5]
list2=[2001,2002,2003,2004,2005]
list3=[0.05, -0.07, -0.08, 0.004, -0.02, -0.03, 0.03, 0.02, 0.06]
So these are my 3 lists that are used
for l2,l1 in zip(list2,list1):
if list3>= 0.04: #where the type error issue has occurred
print(f"{l2} and {l1}")
Any ways to fix this coding or improve it?
–
–
–
–
print(f"{l2} and {l1}")
Notice that lengths of list1 and list2 are smaller than the length of list3. Therefore this for loop won't iterate through the end of the list3.
Output will be:
2001 and 1
–
–
I am not sure what you want to do but I think you want to print the list1
and list2
elements if the list3
element is greater than 0.04
.
If you want to do the above thing, you can use the enumerate for it. The enumerate add the list index to the for
loop.
Code:
list1 = [1, 2, 3, 4, 5]
list2 = [2001, 2002, 2003, 2004, 2005]
list3 = [0.05, -0.07, -0.08, 0.004, -0.02, -0.03, 0.03, 0.02, 0.06]
for idx, l3 in enumerate(list3):
if idx > len(list1) or idx > len(list2):
break
if l3 >= 0.04:
print(f"{list1[idx]} and {list2[idx]}")
Output:
>>> python3 test.py
1 and 2001