Thursday, December 26, 2024
Google search engine
HomeLanguagesPython | Rear Addition of Record

Python | Rear Addition of Record

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




# Python3 code to demonstrate working of
# Rear Addition of Record
# using insert()
 
# Initializing list
test_list = [('is', 2), ('best', 3)]
 
# printing original list
print("The original list is : " + str(test_list))
 
# Initializing tuple to add
add_tuple = ('gfg', 1)
 
# Rear Addition of Record
# using insert()
test_list.insert(len(test_list), add_tuple)
 
# printing result
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




# Python3 code to demonstrate working of
# Rear Addition of Record
# using deque() + append()
from collections import deque
 
# Initializing list
test_list = [('is', 2), ('best', 3)]
 
# printing original list
print("The original list is : " + str(test_list))
 
# Initializing tuple to add
add_tuple = ('gfg', 1)
 
# Rear Addition of Record
# using deque() + append()
res = deque(test_list)
res.append(add_tuple)
 
# printing result
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




# Python3 code to demonstrate working of
# Rear Addition of Record
 
# Initializing list
test_list = [('is', 2), ('best', 3)]
 
# printing original list
print("The original list is : " + str(test_list))
 
# Initializing tuple to add
add_tuple = ('gfg', 1)
 
# Rear Addition of Record
test_list.extend([add_tuple])
# printing result
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




# Python3 code to demonstrate working of
# Rear Addition of Record
 
# Initializing list
test_list = [('is', 2), ('best', 3)]
 
# printing original list
print("The original list is : " + str(test_list))
 
# Initializing tuple to add
add_tuple = ('gfg', 1)
 
# Rear Addition of Record
new_list = [add_tuple]
test_list = test_list + new_list
 
# printing result
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:

  1. Initialize the original list test_list and the tuple to be added add_tuple.
  2. Print the original list.
  3. Add the add_tuple to the test_list using heapq.heappush() method.
  4. Convert the test_list heap back to a list using the list() method.
  5. Print the updated list.

Python3




import heapq
 
# Initializing list
test_list = [('is', 2), ('best', 3)]
 
# printing original list
print("The original list is : " + str(test_list))
 
# Initializing tuple to add
add_tuple = ('gfg', 1)
 
# Add the tuple to the list using heapq
heapq.heappush(test_list, add_tuple)
 
# Convert the heap back to a list
res = list(test_list)
 
# printing result
print("The tuple after adding is : " + str(res))
#This code is contributed by Jyothi pinjala.


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).

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments