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

2
  • 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. Commented Jan 26, 2021 at 13:38
  • It sound like A And B are lists of lists. Is that correct? Commented Jan 26, 2021 at 13:53

1 Answer 1

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']]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.