
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Adding value to sublists in Python
Sometimes we need to introduce an additional value to an already existing list. In this article we will see how the new value or values can be inserted into an already existing list by combining with each item of the existing list.
Using For Loop
If we take a list which has items of the same length, we can use this method to introduce new values in each of the item of the list. In the below example we are taking a list of
Example
List = [[10, 20], [14, 8],['Mon','Tue']] print("Given List: \n" + str(List)) s = "Rise" t = "fast" result = [[m, n, s,t ] for m, n in List] #result print("\nNew List: \n" + str(result))
Running the above code gives us the following result
Output
Given List: [[10, 20], [14, 8], ['Mon', 'Tue']] New List: [[10, 20, 'Rise', 'fast'], [14, 8, 'Rise', 'fast'], ['Mon', 'Tue', 'Rise', 'fast']]
Using + Operator
The + operator when used with a list simply adds new elements to each of the list items. In the below example we find that even a list itself can be used as a new element to be added to the existing lift. Also the existing elements in the list can be of varying length.
Example
List = [[1.5, 2.5, 'Tue'], [0.8, 0.9, 'Ocean'], [6.8, 4.3], [9]] print("Given List: \n" + str(List)) # Choose a list to be added. s = ["Rise","Fast"] result = [sub + [s] for sub in List] print("\nNew List: \n" + str(result))
Running the above code gives us the following result
Output
Given List: [[1.5, 2.5, 'Tue'], [0.8, 0.9, 'Ocean'], [6.8, 4.3], [9]] New List: [[1.5, 2.5, 'Tue', ['Rise', 'Fast']], [0.8, 0.9, 'Ocean', ['Rise', 'Fast']], [6.8, 4.3, ['Rise', 'Fast']], [9, ['Rise', 'Fast']]]
- Related Articles
- Program to maximize the minimum value after increasing K sublists in Python
- Count unique sublists within list in Python
- Python program to print all sublists of a list.
- Adding Time in Python
- Program to find maximum sum of two non-overlapping sublists in Python
- Program to find max values of sublists of size k in Python
- Adding a day to a DATETIME format value in MySQL?
- Add the occurrence of each number as sublists in Python
- Program to find sum of the sum of all contiguous sublists in Python
- Program to find number of sublists whose sum is given target in python
- Program to count number of sublists with exactly k unique elements in Python
- Count the sublists containing given element in a list in Python
- How Digital Marketing is Adding Value to the Business?
- Program to find number of sublists that contains exactly k different words in Python
- Adding Tuple to List and vice versa in Python
