Python | Rear Addition of Record
Last Updated :
22 Apr, 2023
Sometimes, while working with Python list, we can have a problem in which we need to add a new tuple to existing list. Append at rear is usually easier than addition at front. Let’s discuss certain ways in which this task can be performed.
Method #1 : Using insert() This is one of the way in which the element can be added to rear in one-liner. It is used to add any element in rear of list. The behaviour is the same for tuple as well.
Python3
test_list = [( 'is' , 2 ), ( 'best' , 3 )]
print ( "The original list is : " + str (test_list))
add_tuple = ( 'gfg' , 1 )
test_list.insert( len (test_list), add_tuple)
print ( "The tuple after adding is : " + str (test_list))
|
Output
The original list is : [('is', 2), ('best', 3)]
The tuple after adding is : [('is', 2), ('best', 3), ('gfg', 1)]
Time Complexity: O(1) as the insert() method takes constant time for adding the element at the rear end.
Auxiliary Space: O(1) as the size of the list is not increasing, only the reference to the list is changing.
Method #2 : Using deque() + append() The combination of above functions can be used to perform this particular task. In this, we just need to convert the list into a deque so that we can perform the append at right using append().
Python3
from collections import deque
test_list = [( 'is' , 2 ), ( 'best' , 3 )]
print ( "The original list is : " + str (test_list))
add_tuple = ( 'gfg' , 1 )
res = deque(test_list)
res.append(add_tuple)
print ( "The tuple after adding is : " + str ( list (res)))
|
Output
The original list is : [('is', 2), ('best', 3)]
The tuple after adding is : [('is', 2), ('best', 3), ('gfg', 1)]
Time Complexity: O(1)
Auxiliary Space: O(n) where n is the size of the list or the number of elements in the list.
Method #3 : Using extend() method
Python3
test_list = [( 'is' , 2 ), ( 'best' , 3 )]
print ( "The original list is : " + str (test_list))
add_tuple = ( 'gfg' , 1 )
test_list.extend([add_tuple])
print ( "The tuple after adding is : " + str (test_list))
|
Output
The original list is : [('is', 2), ('best', 3)]
The tuple after adding is : [('is', 2), ('best', 3), ('gfg', 1)]
Time complexity: O(1)
Auxiliary Space: O(n) where n is the size of the test_list.
Method 4: Using the + operator to concatenate two lists
Step-by-step approach:
- Initialize the list with tuple elements.
- Create a new list containing the tuple to add.
- Concatenate the two lists using the + operator.
- Assign the concatenated list to the original list variable.
- Print the modified list.
Python3
test_list = [( 'is' , 2 ), ( 'best' , 3 )]
print ( "The original list is : " + str (test_list))
add_tuple = ( 'gfg' , 1 )
new_list = [add_tuple]
test_list = test_list + new_list
print ( "The tuple after adding is : " + str (test_list))
|
Output
The original list is : [('is', 2), ('best', 3)]
The tuple after adding is : [('is', 2), ('best', 3), ('gfg', 1)]
Time Complexity: O(n), where n is the length of the original list.
Auxiliary Space: O(n), where n is the length of the original list.
METHOD 5: Using list comprehension
APPROACH:
This Python program adds a new key-value tuple to a list of key-value tuples. The program uses a list comprehension to concatenate the original list and the new tuple. The new list is then displayed.
ALGORITHM:
1.Initialize the original list with key-value pairs.
2.Create a new tuple with the key-value pair to be added.
3.Use a list comprehension to concatenate the original list and the new tuple.
4.Display the new list.
Python3
original_list = [( 'is' , 2 ), ( 'best' , 3 )]
new_tuple = ( 'gfg' , 1 )
new_list = [item for item in original_list] + [new_tuple]
print ( "The tuple after adding is:" , new_list)
|
Output
The tuple after adding is: [('is', 2), ('best', 3), ('gfg', 1)]
Time complexity: O(n), where n is the length of the original list.
Space complexity: O(n)
METHOD 6: Using heapq:
Algorithm:
- Initialize the original list test_list and the tuple to be added add_tuple.
- Print the original list.
- Add the add_tuple to the test_list using heapq.heappush() method.
- Convert the test_list heap back to a list using the list() method.
- Print the updated list.
Python3
import heapq
test_list = [( 'is' , 2 ), ( 'best' , 3 )]
print ( "The original list is : " + str (test_list))
add_tuple = ( 'gfg' , 1 )
heapq.heappush(test_list, add_tuple)
res = list (test_list)
print ( "The tuple after adding is : " + str (res))
|
Output
The original list is : [('is', 2), ('best', 3)]
The tuple after adding is : [('gfg', 1), ('best', 3), ('is', 2)]
Time Complexity:
The time complexity of heappush() method is O(log n) where n is the number of elements in the heap. In this case, we are adding only one element to the heap, so the time complexity would be O(log 2) which is a constant time operation.
Converting the heap back to a list takes O(n) time complexity, where n is the number of elements in the heap.
Space Complexity:
The space complexity of the algorithm is O(n) where n is the number of elements in the heap. This is because heappush() method creates a heap data structure which takes O(n) space. However, since we are adding only one element to the heap, the actual space used in this case would be very small. Additionally, the space used by the list is also O(n).
Similar Reads
Python | Addition of tuples
Sometimes, while working with records, we might have a common problem of adding contents of one tuple with the corresponding index of other tuple. This has application in almost all the domains in which we work with tuple records. Let's discuss certain ways in which this task can be performed. Metho
5 min read
Python | Append at front and remove from rear
We are given a list we need to append element in front and remove from rear. For example, a = [10, 20, 30] we need to add 5 in front and remove from rear so that resultant output should be [5, 10, 20]. Using insertUsing insert in Python allows us to add an element at the front of a list by specifyin
2 min read
Python | Records List Concatenation
Sometimes, while working with Python records, we can have a problem in which we need to perform cross concatenation of list of tuples. This kind of application is popular in web development domain. Letâs discuss certain ways in which this task can be performed. Method #1 : Using list comprehension +
4 min read
Python - Rear element extraction from list of tuples records
While working with tuples, we store different data as different tuple elements. Sometimes, there is a need to print specific information from the tuple like rear index. For instance, a piece of code would want just names to be printed on all the student data. Let's discuss certain ways in which one
8 min read
Python - Add Values to Dictionary of List
A dictionary of lists allows storing grouped values under specific keys. For example, in a = {'x': [10, 20]}, the key 'x' maps to the list [10, 20]. To add values like 30 to this list, we use efficient methods to update the dictionary dynamically. Letâs look at some commonly used methods to efficien
3 min read
Python | Add dictionary to tuple
Sometimes, while working with data, we can have a problem in which we need to append to a tuple a new record which is of form of Python dictionary. This kind of application can come in web development domain in case of composite attributes. Let's discuss certain ways in which this task can be perfor
4 min read
Python | Alternate Rear iteration
The iteration of numbers is done by looping techniques in python. There are many techniques in Python which facilitate looping. Sometimes we require to perform the looping backwards in alternate way and having shorthands to do so can be quite useful. Letâs discuss certain ways in which this can be d
3 min read
Access front and rear element of Python tuple
Sometimes, while working with records, we can have a problem in which we need to access the initial and last data of a particular record. This kind of problem can have application in many domains. Let's discuss some ways in which this problem can be solved. Method #1: Using Access Brackets We can pe
6 min read
Addition in Nested Tuples - Python
Sometimes, while working with records, we can have a problem in which we require to perform index wise addition of tuple elements. This can get complicated with tuple elements to be tuple and inner elements again be tuple. Let's discuss certain ways in which this problem can be solved. Method #1: Us
6 min read
How to Update a Dictionary in Python
This article explores updating dictionaries in Python, where keys of any type map to values, focusing on various methods to modify key-value pairs in this versatile data structure. Update a Dictionary in PythonBelow, are the approaches to Update a Dictionary in Python: Using with Direct assignmentUs
3 min read