Python | Adding value to sublists
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 sacrifice 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
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 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)) |
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’]]
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 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)) |
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’]]
Recommended Posts:
- Python | Swapping sublists over given range
- Python | Merge elements of sublists
- Python | Add the occurrence of each number as sublists
- Python | Merge corresponding sublists from two different lists
- Python | Remove all sublists outside the given range
- Python | Print all sublists of a list
- Python | Sort all sublists in given list of strings
- Python | Count unique sublists within list
- Python | Remove sublists that are present in another sublist
- Python | Remove repeated sublists from given list
- Python | Split a list into sublists of given lengths
- Python | Count the sublists containing given element in a list
- Python | Print the common elements in all sublists
- Python | Sort list of lists by the size of sublists
- Python | Split list of strings into sublists based on length
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



