Reputation: 105
Hypothetically, say I have two lists that have data as follows, and list A
has multiple rows that I write into a csv via a for
loop.
A = [Date, Round Number, Score, FirstName]
B = [FirstName, Surname]
Where every row of A
is a different round in a sports tournament. List B
contains every player's FirstName
and Surname
. This data is gathered from a GraphQL API query.
There are 100 rows of A
in the CSV (100 rounds), and List B
has 20 rows (20 players).
Question:
How would I be able to append a player's Surname
into List A
after every instance of their FirstName
?
Disclaimer: I have no background in code, this is my first attempt at a python project.
I've checked out this post but I have not been able to find one without replacing. Update list elements based on a second list as index
Upvotes: 0
Views: 44
Reputation: 2132
Code below will solver your problem:
A = [["Date", "Round Number", "Score", "FirstNameA"],
["Date", "Round Number", "Score", "FirstNameB"],
["Date", "Round Number", "Score", "FirstNameC"]]
B = [["FirstNameC", "SurnameC"],
["FirstNameA", "SurnameA"],
["FirstNameB", "SurnameB"]]
for round in A:
for player in B:
if player[0] == round[-1]:
round[-1] += " " + player[-1] #Append to last index of round to last index of player
print(A)
Output:
[['Date', 'Round Number', 'Score', 'FirstNameA SurnameA'], ['Date', 'Round Number', 'Score', 'FirstNameB SurnameB'], ['Date', 'Round Number', 'Score', 'FirstNameC SurnameC']]
Upvotes: 2