0

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

corybantic
  • 105
  • 2
  • 12
  • see python pandas . If you have no coding background then searching is a better option than asking. So use stackoverflow as a searching tool than asking tool. You can ask on Gitter, IRC Freenode, Discord etc if you are a beginner. – Vishesh Mangla Jan 26 '21 at 13:38
  • It sound like A And B are lists of lists. Is that correct? – pakpe Jan 26 '21 at 13:53

1 Answers1

2

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']]
Jakub Szlaur
  • 1,852
  • 10
  • 39