EDITED: In me trying to exemplify the coding issue I made a mistake in regards to my desired outcome. I have updated the outcome to my ACTUAL desired outcome.
I have a list of lists called list_of_lists_1
that I whish to loop over in parallel with a
sister list (if that is a term in coding) called list_of_lists_2
, and a third list called person_list
. list_of_lists_1
and list_of_lists_2
have the same number of elements both in total and within each list of lists. Each element in person_list
should loop with its respective list in list_of_lists_1
and list_of_lists_2
i.e. person_a
will loop in tandem with lists [1, 3, 5]
and [2, 4, 6]
. Just as persin_b
will loop with ["7", "9", "11"]
and ["8", "10", "12"]
etc.
# create lists
list_of_lists_1 = [["1", "3", "5"], ["7", "9", "11"], ["13", "15", "17"]]
list_of_lists_2 = [["2", "4", "6"], ["8", "10", "12"], ["14", "16", "18"]]
person_list = ["person_a", "person_b", "person_c"]
I have tried the following code. However, the output only amounts to about 50 pct. of the desired outcome.
# loop
for i in range(0, len(list_of_lists_1)):
outcome = person_list[i] list_of_lists_1[i][i] list_of_lists_2[i][i]
# some functions and other interesting stuff!
print(outcome)
person_a12
person_b910
person_c1718
# desired outcome (Has been edited)
person_a12
person_a34
person_a56
person_b78
person_b910
person_b1112
person_c1314
person_c1516
person_c1718
How would I achieve the desired outcome?
Thank you.
CodePudding user response:
Use zip()
to loop over lists in parallel.
Then you can use a nested loop to combine the elements of the nested lists in list_of_lists_X
.
Given:
list_of_lists_1 = [["1", "3", "5"], ["7", "9", "11"], ["13", "15", "17"]]
list_of_lists_2 = [["2", "4", "6"], ["8", "10", "12"], ["14", "16", "18"]]
person_list = ["person_a", "person_b", "person_c"]
Your result can be constructed via:
for suffixes_1, suffixes_2, name in zip(list_of_lists_1, list_of_lists_2, person_list):
for s1, s2 in zip(suffixes_1, suffixes_2):
outcome = name s1 s2
print(outcome)
CodePudding user response:
@Barmar's solution is a little more condensed but I'll post this anyway.
list_of_lists_1 = [["1", "3", "5"], ["7", "9", "11"], ["13", "15", "17"]]
list_of_lists_2 = [["2", "4", "6"], ["8", "10", "12"], ["14", "16", "18"]]
person_list = ["person_a", "person_b", "person_c"]
for n,p in enumerate(person_list):
output = p
for digits in zip(list_of_lists_1[n],list_of_lists_2[n]):
output = digits[0] digits[1]
print(output)
CodePudding user response:
By extension of @Barmar's answer (https://stackoverflow.com/a/71243300/218663) here is how you might do it with a list comprehension:
list_of_lists_1 = [["1", "3", "5"], ["7", "9", "11"], ["13", "15", "17"]]
list_of_lists_2 = [["2", "4", "6"], ["8", "10", "12"], ["14", "16", "18"]]
person_list = ["person_a", "person_b", "person_c"]
result = (
f"{name}{s1}{s2}"
for suffixes_1, suffixes_2, name in zip(list_of_lists_1, list_of_lists_2, person_list)
for s1, s2 in zip(suffixes_1, suffixes_2)
)
for outcome in result:
print(outcome)
Giving you:
person_a12
person_a34
person_a56
person_b78
person_b910
person_b1112
person_c1314
person_c1516
person_c1718