Python | Add tuple to front of list (original) (raw)

Last Updated : 08 May, 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 front in one-liner. It is used to add any element in front 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( 0 , 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 : [('gfg', 1), ('is', 2), ('best', 3)]

Time complexity: O(n) where n is the number of elements in the list as we are inserting an element in the front of the list which takes linear time.
Auxiliary Space: O(1) as we are not using any extra data structure and only inserting an element in the existing list.

Method #2 : Using deque() + appendleft()

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 front using appendleft()

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.appendleft(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 : [('gfg', 1), ('is', 2), ('best', 3)]

Time complexity: O(1), where n is the length of the list.
Auxiliary space: O(1)

Method #3 : Using extend() method

Python3

test_list = [( 'is' , 2 ), ( 'best' , 3 )]

print ( "The original list is : " + str (test_list))

add_tuple = ( 'gfg' , 1 )

x = [add_tuple]

x.extend(test_list)

print ( "The tuple after adding is : " + str (x))

Output

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

Time Complexity: O(k), Where k is the length of the list that needs to be added.
Auxiliary Space: O(k)

Method #4: Using the concatenation operator
You can use the + operator to concatenate the list and the tuple, and it will add the tuple to the front of the list.

Python3

test_list = [( 'is' , 2 ), ( 'best' , 3 )]

print ( "The original list is : " + str (test_list))

add_tuple = ( 'gfg' , 1 )

test_list = [add_tuple] + test_list

print ( "The tuple after adding is : " + str (test_list))

Output

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

Time Complexity: O(k), Where k is the length of the list that needs to be added.
Auxiliary Space: O(k)

Method #5 : Using slicing and unpacking

This method creates a new list that consists of the new tuple followed by the existing list, using the * operator to unpack the elements of the existing list.

Python3

test_list = [( 'is' , 2 ), ( 'best' , 3 )]

print ( "The original list is : " + str (test_list))

add_tuple = ( 'gfg' , 1 )

test_list = [add_tuple, * test_list]

print ( "The tuple after adding is : " + str (test_list))

Output

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

Time complexity: O(n), where n is the length of the list, because creating a new list using slicing and unpacking requires iterating over all the elements of the existing list.
Auxiliary space: O(n+1), because it creates a new list that is one element longer than the original list. The additional element is the new tuple that is being added to the front of the list.

Method 6: Using list comprehension and the append() method

  1. Initialize the list test_list and print it.
  2. Initialize the tuple add_tuple and print it.
  3. Use a list comprehension to create a new list with the add_tuple at the beginning and the rest of the elements from test_list.
  4. Use the append() method to add each element from the new list to test_list.
  5. Print the updated list.

Python3

test_list = [( 'is' , 2 ), ( 'best' , 3 )]

print ( "The original list is : " + str (test_list))

add_tuple = ( 'gfg' , 1 )

test_list = [add_tuple] + [i for i in test_list]

print ( "The tuple after adding is : " + str (test_list))

test_list = [( 'is' , 2 ), ( 'best' , 3 )]

print ( "The original list is : " + str (test_list))

add_tuple = ( 'gfg' , 1 )

new_list = [add_tuple] + [i for i in test_list]

for i in new_list:

`` test_list.append(i)

print ( "The tuple after adding is : " + str (test_list))

Output

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

Time complexity: O(n) for both methods as we need to create a new list and append elements to the existing list.
Auxiliary space: O(n) for the first method and O(2n) for the second method.

Method #7 : Using the unpacking operator (*) and the list() constructor

  1. Initialize the list “test_list” with the tuples
  2. Initialize the tuple “add_tuple” with the tuple to add to the front of the list
  3. Create a new list using the unpacking operator (*) to unpack the “add_tuple” and the original list “test_list”
  4. Use the list() constructor to create a new list from the unpacked elements
  5. 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 = 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 : [('gfg', 1), ('is', 2), ('best', 3)]

Time complexity: O(n) (where n is the length of the list)
Auxiliary space: O(n)