Python | Adding value to sublists (original) (raw)

Sometimes, we just have to manipulate a list of lists by appending a similar value to all the sublists. Using a loop for achieving this particular task can be an option but sometimes leads to sacrificing the readability of code. It is always wanted to have a oneliner to perform this particular task. Let's discuss certain ways in which this can be done.

Method #1: Using list comprehension can be used to perform this particular task using a similar looping construct but in just a single line. This increases code readability.

Python3 `

Python3 code to demonstrate

appending single value

using list comprehension

initializing list of lists

test_list = [[1, 3], [3, 4], [6, 5], [4, 5]]

printing original list

print("The original list : " + str(test_list))

declaring element to be inserted

K = "GFG"

using list comprehension

appending single value

res = [[i, j, K] for i, j in test_list]

printing result

print("The list after adding element : " + str(res))

`

Output

The original list : [[1, 3], [3, 4], [6, 5], [4, 5]] The list after adding element : [[1, 3, 'GFG'], [3, 4, 'GFG'], [6, 5, 'GFG'], [4, 5, 'GFG']]

Time complexity: O(n), where n is the number of elements in the original list (test_list).
Auxiliary space: O(n), as a new list (res) is created with the same number of elements as the original list.

Method #2 : Using list comprehension + "+" operator This method is quite similar to the above method, but the difference is that plus operator is used to add the new element to each sublist.

Python3 `

Python3 code to demonstrate

appending single value

using list comprehension + "+" operator

initializing list of lists

test_list = [[1, 3], [3, 4], [6, 5], [4, 5]]

printing original list

print("The original list : " + str(test_list))

declaring element to be inserted

K = "GFG"

using list comprehension + "+" operator

appending single value

res = [sub + [K] for sub in test_list]

printing result

print("The list after adding element : " + str(res))

`

Output

The original list : [[1, 3], [3, 4], [6, 5], [4, 5]] The list after adding element : [[1, 3, 'GFG'], [3, 4, 'GFG'], [6, 5, 'GFG'], [4, 5, 'GFG']]

Time complexity: O(n) where n is the number of sublists in the test_list.
Auxiliary space: O(n) for the new list 'res' created using the list comprehension.

Method #3 : Using for loop

Python3 `

Python3 code to demonstrate

appending single value

initializing list of lists

test_list = [[1, 3], [3, 4], [6, 5], [4, 5]]

printing original list

print("The original list : " + str(test_list))

declaring element to be inserted

K = "GFG" res = [] for i in test_list: i.append(K) res.append(i)

printing result

print("The list after adding element : " + str(res))

`

Output

The original list : [[1, 3], [3, 4], [6, 5], [4, 5]] The list after adding element : [[1, 3, 'GFG'], [3, 4, 'GFG'], [6, 5, 'GFG'], [4, 5, 'GFG']]

Time complexity: O(n), where n is the length of the input list.
Auxiliary space: O(n), where n is the length of the input list.

Method #4: Using map() and lambda function

Here is the approach using map() and lambda function

Python3 `

Python3 code to demonstrate

appending single value

initializing list of lists

test_list = [[1, 3], [3, 4], [6, 5], [4, 5]]

printing original list

print("The original list : " + str(test_list))

Declaring element to be inserted

K = "GFG"

Using map() and lambda function to add element to all sublists

res = list(map(lambda x: x + [K], test_list)) print("The list after adding element : " + str(res))

#This code is contributed by Edula Vinay Kumar Reddy

`

Output

The original list : [[1, 3], [3, 4], [6, 5], [4, 5]] The list after adding element : [[1, 3, 'GFG'], [3, 4, 'GFG'], [6, 5, 'GFG'], [4, 5, 'GFG']]

Time complexity: O(n)
Auxiliary Space: O(n)

Method #5: Using itertools.chain and zip:

Python3 `

import itertools # Import the itertools module test_list = [[1, 3], [3, 4], [6, 5], [4, 5]] # Initialize the list of lists K = "GFG" # Declare the element to be inserted

printing original list

print("The original list : " + str(test_list)) res = [list(itertools.chain(i, [K])) for i in test_list]

Use list comprehension and itertools.chain to add the element K to each sublist in the list of lists

itertools.chain takes multiple iterables as arguments and returns a single, flatten iterable that contains elements from all the input iterables

Here, i is each sublist in test_list, and [K] is a list with a single element K

The result of itertools.chain(i, [K]) is a flatten list that contains all elements in i followed by K

The list comprehension [list(itertools.chain(i, [K])) for i in test_list] applies this operation to each sublist in test_list and collects the results into a new list of lists

print("The list after adding element : " + str(res)) # Print the result

This code is contributed by Jyothi pinjala.

`

Output

The original list : [[1, 3], [3, 4], [6, 5], [4, 5]] The list after adding element : [[1, 3, 'GFG'], [3, 4, 'GFG'], [6, 5, 'GFG'], [4, 5, 'GFG']]

Time complexity: O(n)
Auxiliary Space: O(n*k)

Method #6: Using numpy
To use numpy we need to install it first. You can install it using the following command: pip install numpy

Step by step approach:

import numpy as np

initializing list of lists

test_list = [[1, 3], [3, 4], [6, 5], [4, 5]]

printing original list

print("The original list : " + str(test_list))

declaring element to be inserted

K = "GFG"

using numpy

appending single value

test_np = np.array(test_list) res_np = np.append(test_np, np.full((len(test_list), 1), K), axis=1).reshape(len(test_list), -1).tolist()

printing result

print("The list after adding element : " + str(res_np))

`

Output:

The original list : [[1, 3], [3, 4], [6, 5], [4, 5]] The list after adding element : [['1', '3', 'GFG'], ['3', '4', 'GFG'], ['6', '5', 'GFG'], ['4', '5', 'GFG']]

Time complexity: O(n) (where n is the number of elements in the list)
Auxiliary space: O(n) (for creating a numpy array)

Method #7: Using extend() method inside a for loop

Use a for loop to iterate through each sublist of the original list.
Use the extend() method to append the value to each sublist.

Python3 `

test_list = [[1, 3], [3, 4], [6, 5], [4, 5]] K = "GFG"

for sublist in test_list: sublist.extend([K])

print("The list after adding element : " + str(test_list))

`

Output

The list after adding element : [[1, 3, 'GFG'], [3, 4, 'GFG'], [6, 5, 'GFG'], [4, 5, 'GFG']]

Time complexity: O(n), where n is the length of the list.
Auxiliary space: O(1), because no additional data structure is created.