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.

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output :



The original list is : [('is', 2), ('best', 3)]
The tuple after adding is : [('is', 2), ('best', 3), ('gfg', 1)]

 

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

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output :

The original list is : [('is', 2), ('best', 3)]
The tuple after adding is : [('is', 2), ('best', 3), ('gfg', 1)]



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.