Peter_uk
Peter_uk

Reputation: 35

Moving last section of list two to the left

short question about moving the last item of a list inside a nested list two to the left for example:

list = [['Swimming', 'Jason', 'jsmg1', '2', '1352452'], 
       ['Football', 'Chris', 'cfb1', '1', '1352527'],
       ['Tennis', 'J', 'Paul', 'pten1', '2', '1132624']]

to

list = [['Swimming', 'Jason', '1352452', 'jsmg1, '2'], 
       ['Football', 'Chris', '1352527', 'cfb1', '1'],
       ['Tennis', 'J', 'Paul', '1132624', 'pten1', '2']]

how can this be done? Thank you

Upvotes: 2

Views: 30

Answers (1)

TigerhawkT3
TigerhawkT3

Reputation: 49330

Use slice assignment.

l = [['Swimming', 'Jason', 'jsmg1', '2', '1352452'], 
       ['Football', 'Chris', 'cfb1', '1', '1352527'],
       ['Tennis', 'J', 'Paul', 'pten1', '2', '1132624']]

for row in l:
    row[2:] = row[4], row[2], row[3]

The result:

>>> import pprint
>>> pprint.pprint(l)
[['Swimming', 'Jason', '1352452', 'jsmg1', '2'],
 ['Football', 'Chris', '1352527', 'cfb1', '1'],
 ['Tennis', 'J', '2', 'Paul', 'pten1']]

Upvotes: 3

Related Questions