I understand this code is running properly and is grabbing from each excel file individually, but I want to create a for loop for using this data across multiple excel files

kinematic_viscosity = []

kinematic_viscosity_1= []

kinematic_viscosity_2= []
#*empty arrays for creating after loop in completed*
for dynamic_viscosity in DV:
   kinematic_viscosities = dynamic_viscosity/Density_PE_298k

for dynamic_viscosity_1 in DV_1:

    kinematic_viscosities_1 = dynamic_viscosity_1/Density_PE_298k
#second loop 
for dynamic_viscosity_2 in DV_2:

    kinematic_viscosities_2 = dynamic_viscosity_2/Density_PE_298k 
#third loop

One possible solution is using zip(). This allows you to iterate, in your case, through multiple lists at a time. You can also condense creating the lists into one line. And, you can remove assigning dynamic_viscosity_#/Density_PE_298k to a variable, just append that to the lists. Consider the following:

kinematic_viscosity, kinematic_viscosity_1, kinematic_viscosity_2 = [], [], []

for dynamic_viscosity, dynamic_viscosity_1, dynamic_viscosity_2 in zip(DV, DV_1, DV_2):

While it looks more blocky, it allows you to only loop through the lists once.

Another solution is to keep the three loops, but append the returns directly into the list. Consider the following:

kinematic_viscosity = [dynamic_viscosity/Density_PE_298k for dynamic_viscosity in DV]

kinematic_viscosity_1= [dynamic_viscosity_1/Density_PE_298k for dynamic_viscosity_1 in DV_1]

kinematic_viscosity_2= [dynamic_viscosity_2/Density_PE_298k for dynamic_viscosity_2 in DV_2]
  • \$\begingroup\$ Thank you! worked! \$\endgroup\$ – Jacob Saxinger Sep 18 '19 at 15:08

Not the answer you're looking for? Browse other questions tagged or ask your own question.