0

I am trying to figure out how to append multiple values to a list in Python. I've try for loop and append but i'm really stuck.

Here my list Array:

array_1 = [[('Manchester', '23', '80'),
  ('Manchester', '22', '79'),
  ('Manchester', '19', '76')],
 [('Benfica', '26', '77'),
  ('Benfica', '22', '74'),
  ('Benfica', '17', '70')],
 [('Dortmund', '24', '75'),
  ('Dortmund', '18', '74'),
  ('Dortmund', '16', '69')]
]

array_2 = [[('Manchester', '23', 'CM'),
  ('Manchester', '22', 'RM'),
  ('Manchester', '19', 'LB')],
 [('Benfica', '26', 'CF'),
  ('Benfica', '22', 'CDM'),
  ('Benfica', '17', 'RB')],
 [('Dortmund', '24', 'CM'),
  ('Dortmund', '18', 'AM'),
  ('Dortmund', '16', 'LM')]
]

I expected the outcome:

result = [[('Manchester', '23', '80', 'CM'),
  ('Manchester', '22', '79', 'RM'),
  ('Manchester', '19', '76', 'LB')],
 [('Benfica', '26', '77', 'CF'),
  ('Benfica', '22', '74', 'CDM'),
  ('Benfica', '17', '70', 'RB')],
 [('Dortmund', '24', '75', 'CM'),
  ('Dortmund', '18', '74', 'AM'),
  ('Dortmund', '16', '69', 'LM')]
]

i need to save the result into csv with header:

club | age | overall | position
3
  • your first array is cropped, fix it please Commented Sep 21, 2020 at 9:26
  • @YossiLevi sorry, i've fixed Commented Sep 21, 2020 at 11:20
  • the output is array_2 . what is the process you want done? Commented Sep 27, 2020 at 14:56

1 Answer 1

1

The following should work:

new_list=[]
for i in zip(array_1, array_2):
    temp=[]
    for k in zip(i[0], i[1]):
        temp.append(k[0]+(k[1][2],))
    new_list.append(temp)
print(new_list)

Output:

[[('Manchester', '23', '80', 'CM'), ('Manchester', '22', '79', 'RM'), ('Manchester', '19', '76', 'LB')], [('Benfica', '26', '77', 'CF'), ('Benfica', '22', '74', 'CDM'), ('Benfica', '17', '70', 'RB')], [('Dortmund', '24', '75', 'CM'), ('Dortmund', '18', '74', 'AM'), ('Dortmund', '16', '69', 'LM')]]

Sign up to request clarification or add additional context in comments.

1 Comment

I tried .extend and it worked perfectly. Thanks for the help

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.