Updating a 3D list using python list comprehension

We will see how to do this for two types of data. A list that contains other lists within and a list that contains key-value pairs (dictionaries) within.

  • A list of lists without keys

    In this sample, we will implement an update on a three-dimensional list using List Comprehension. Below sample has 3 levels of data.

    Main student list has multiple students with id and name. Within each of these lists, there is another list, with marks in 3 subjects. We will try updating the third level list with total marks using a single line of code.

    Copied
    studentsList = [
        [1, 'Carl', 
            [48, 47, 45]
        ],
        [2, 'Tim', 
            [43, 42, 41]
        ],
        [3, 'Peter', 
            [40, 44, 47]
        ]
    ]
    
    #Display total marks in the second level and removing third level
    totalMarks = [ [ st[0], st[1], st[2][0] + st[2][1] + st[2][2] ]  for st in studentsList]
    print(totalMarks)
    #Keeping third level and display total as another column
    totalMarks = [ [ st[0], st[1], st[2], st[2][0] + st[2][1] + st[2][2] ]  for st in studentsList]
    print(totalMarks)
    
    Outputs:

    Total displayed on second level. No third level.
    [
    	[1, 'Carl', 140],
    	[2, 'Tim', 126],
    	[3, 'Peter', 131]
    ]
    
    Total displayed on second level. Third level still there.
    [
    	[1, 'Carl', 
    		[48, 47, 45], 140],
    	[2, 'Tim',
    		[43, 42, 41], 126],
    	[3, 'Peter', 
    		[40, 44, 47], 131]
    ]
    
  • If list has data in the form of key-value pairs

    For bigger lists, the above format is difficult to manage while coding, because we can't link any name to the column. To handle this issue, we can keep data as key-value pairs.

    Just a single line of code is enough to do an update on this list. Note that we have to use a python library named operator and call the operator.setitem() function, to update the data. In this sample we are finding the total of 3 subjects and adding this using a new key.

    Copied
    import operator
    
    studentsList = [
        {'id': 1, 'name': 'Carl', 
            'marks': {
                    'english': 48, 'science': 47, 'maths': 45}
        },
        {'id': 2, 'name': 'Tim', 
            'marks': {
                    'english': 43, 'science': 42, 'maths': 41}
        },
        {'id': 3, 'name': 'Peter', 
            'marks': {
                    'english': 40, 'science': 44, 'maths': 47}
        }
    ]
    
    [operator.setitem(st, "total", 
                st['marks']['english'] +
                st['marks']['science'] +
                st['marks']['maths'] ) for st in studentsList]
    
    print(studentsList)
    
    Output:
    [
    	{'id': 1, 'name': 'Carl',
    		'marks': {'english': 48, 'science': 47, 'maths': 45},'total': 140},
    	{'id': 2, 'name': 'Tim',
    		'marks': {'english': 43, 'science': 42, 'maths': 41}, 'total': 126},
    	{'id': 3, 'name': 'Peter',
    		'marks': {'english': 40, 'science': 44, 'maths': 47}, 'total': 131}
    ]
    
Absolute Code Works - Python Topics