Python | Size and element exponentiation of list
Last Updated :
05 May, 2023
Sometimes, while working with Python lists, we can have a problem in which we need to extend a list in a very customized way. We may have to repeat the contents of the list and while doing that, each time new list must be exponentiation of the original list. This incremental expansion has applications in many domains. Let’s discuss a way in which this task can be performed.
Method 1: Using list comprehension This task can be performed in a brute manner, but having a shorter implementation using list comprehension always is better. In this, we perform task in 2 steps, first we make a helper list to form an exponentiation factor list and then cumulate the result using the original list.
Python3
test_list = [ 4 , 5 , 6 ]
print ( "The original list is : " + str (test_list))
N = 4
M = 3
temp = [ 1 * M * * i for i in range (N)]
res = list ([ele * * tele for tele in temp for ele in test_list])
print ( "List after extension and exponentiation : " + str (res))
|
Output
The original list is : [4, 5, 6]
List after extension and exponentiation : [4, 5, 6, 64, 125, 216, 262144, 1953125, 10077696, 18014398509481984, 7450580596923828125, 1023490369077469249536]
Time complexity: O(N * M)
Auxiliary Space: O(N * M)
Method 2: Using while loop and extend() method
Python3
test_list = [ 4 , 5 , 6 ]
print ( "The original list is : " + str (test_list))
N = 4
M = 3
j = 1
res = []
res.extend(test_list)
while (j < N):
a = []
for i in test_list:
a.append(i * * 3 )
res.extend(a)
test_list = a
j + = 1
print ( "List after extension and exponentiation : " + str (res))
|
Output
The original list is : [4, 5, 6]
List after extension and exponentiation : [4, 5, 6, 64, 125, 216, 262144, 1953125, 10077696, 18014398509481984, 7450580596923828125, 1023490369077469249536]
Time complexity: O(n^2)
Auxiliary space: O(n^2)
Method 3: Using list comprehension and reduce() function
This method uses the reduce() function to iterate N-1 times and extend the list with the cubed values of the last len(test_list) elements of the list. It uses a lambda function to define the logic of the extension.
Python3
from functools import reduce
test_list = [ 4 , 5 , 6 ]
print ( "The original list is : " + str (test_list))
N = 4
M = 3
res = reduce ( lambda x, _: x + [e * * M for e in x[ - len (test_list):]], range (N - 1 ), test_list)
print ( "List after extension and exponentiation : " + str (res))
|
Output
The original list is : [4, 5, 6]
List after extension and exponentiation : [4, 5, 6, 64, 125, 216, 262144, 1953125, 10077696, 18014398509481984, 7450580596923828125, 1023490369077469249536]
Time complexity: O(NM)
Auxiliary space: O(NM).
METHOD 4 : Using recursion.
Step by step approach:
Define a function that takes a list, extension factor (N), and exponentiation factor (M) as parameters.
Check if N is greater than 1. If yes, then recurse by calling the function again with the updated list obtained by raising each element to the power M, N-1 as the new extension factor, and M as the exponentiation factor.
If N is equal to 1, return the original list.
Finally, return the extended list obtained from the recursive calls.
Python3
test_list = [ 4 , 5 , 6 ]
print ( "The original list is : " + str (test_list))
N = 4
M = 3
def extend_and_exponentiate(lst, n, m):
if n > 1 :
new_lst = [i * * m for i in lst]
return lst + extend_and_exponentiate(new_lst, n - 1 , m)
else :
return lst
res = extend_and_exponentiate(test_list, N, M)
print ( "List after extension and exponentiation : " + str (res))
|
Output
The original list is : [4, 5, 6]
List after extension and exponentiation : [4, 5, 6, 64, 125, 216, 262144, 1953125, 10077696, 18014398509481984, 7450580596923828125, 1023490369077469249536]
Time Complexity: O(NM) where N is the extension factor and M is the exponentiation factor, as we are raising each element to the power M N times.
Auxiliary Space: O(NM) as the size of the extended list will be N*M.
Similar Reads
Python Tutorial | Learn Python Programming Language
Python Tutorial â Python is one of the most popular programming languages. Itâs simple to use, packed with features and supported by a wide range of libraries and frameworks. Its clean syntax makes it beginner-friendly. Python is: A high-level language, used in web development, data science, automat
10 min read
Python Interview Questions and Answers
Python is the most used language in top companies such as Intel, IBM, NASA, Pixar, Netflix, Facebook, JP Morgan Chase, Spotify and many more because of its simplicity and powerful libraries. To crack their Online Assessment and Interview Rounds as a Python developer, we need to master important Pyth
15+ min read
Python OOPs Concepts
Object Oriented Programming is a fundamental concept in Python, empowering developers to build modular, maintainable, and scalable applications. By understanding the core OOP principles (classes, objects, inheritance, encapsulation, polymorphism, and abstraction), programmers can leverage the full p
11 min read
Python Projects - Beginner to Advanced
Python is one of the most popular programming languages due to its simplicity, versatility, and supportive community. Whether youâre a beginner eager to learn the basics or an experienced programmer looking to challenge your skills, there are countless Python projects to help you grow. Hereâs a list
10 min read
Python Exercise with Practice Questions and Solutions
Python Exercise for Beginner: Practice makes perfect in everything, and this is especially true when learning Python. If you're a beginner, regularly practicing Python exercises will build your confidence and sharpen your skills. To help you improve, try these Python exercises with solutions to test
9 min read
Python Programs
Practice with Python program examples is always a good choice to scale up your logical understanding and programming skills and this article will provide you with the best sets of Python code examples. The below Python section contains a wide collection of Python programming examples. These Python c
11 min read
Python Data Types
Python Data types are the classification or categorization of data items. It represents the kind of value that tells what operations can be performed on a particular data. Since everything is an object in Python programming, Python data types are classes and variables are instances (objects) of thes
10 min read
Enumerate() in Python
enumerate() function adds a counter to each item in a list or other iterable. It turns the iterable into something we can loop through, where each item comes with its number (starting from 0 by default). We can also turn it into a list of (number, item) pairs using list(). Let's look at a simple exa
3 min read
Python Lists
In Python, a list is a built-in dynamic sized array (automatically grows and shrinks). We can store all types of items (including another list) in a list. A list may contain mixed type of items, this is possible because a list mainly stores references at contiguous locations and actual items maybe s
6 min read
Python Introduction
Python was created by Guido van Rossum in 1991 and further developed by the Python Software Foundation. It was designed with focus on code readability and its syntax allows us to express concepts in fewer lines of code. Key Features of PythonPythonâs simple and readable syntax makes it beginner-frie
3 min read