Python | Add similar value multiple times in list
Last Updated :
17 Apr, 2023
Adding a single value in list is quite generic and easy. But to add that value more than one time, generally, a loop is used to execute this task. Having shorter tricks to perform this can be handy. Let's discuss certain ways in which this can be done.
Method #1 : Using * operator We can employ * operator to multiply the occurrence of the particular value and hence can be used to perform this task of adding value multiple times in just a single line and makes it readable.
Python3
# Python3 code to demonstrate
# to add multiple similar values
# using * operator
# using * operator to add multiple values
# adds 3, 50 times.
res = [3] * 50
# printing result
print ("The filtered list is : " + str(res))
Output :
The filtered list is : [3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3]
Method #2 : Using extend() + list comprehension extend function is used to perform the list append and list comprehension part is responsible for performing the task of repetition of elements desired number of times.
Python3
# Python3 code to demonstrate
# to add multiple similar values
# using extend() + list comprehension
# using extend() + list comprehension to add multiple values
# adds 3, 50 times.
res = []
res.extend([3 for i in range(50)])
# printing result
print ("The filtered list is : " + str(res))
Output :
The filtered list is : [3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3]
Time Complexity: O(n), where n is the length of the input list. This is because we’re using extend() + list comprehension which has a time complexity of O(n) in the worst case.
Auxiliary Space: O(n), as we’re using additional space res other than the input list itself with the same size of input list.
Method #3 : Using extend() + itertools.repeat() This is similar to the above method, the task of extend() is similar, but repeat() performs the task list comprehension performed of iteration N no. of times desired.
Python3
# Python3 code to demonstrate
# to add multiple similar values
# using extend() + itertools.repeat()
from itertools import repeat
# using extend() + itertools.repeat() to add multiple values
# adds 3, 50 times.
res = []
res.extend(repeat(3, 50))
# printing result
print ("The filtered list is : " + str(res))
Output :
The filtered list is : [3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3]
Method#4: Using Recursive method.
Algorithm:
- Define a function add_multiple_values(val, n, lst) that takes a value val, an integer n, and a list lst.\
- If n is equal to 0, return lst.
- Append val to the end of the list lst using the append() method.
- Decrement n by 1.
- Call add_multiple_values(val, n, lst).
- Return the modified list.
Python3
def add_multiple_values(val, n, lst):
if n == 0:
return lst
else:
lst.append(val)
return add_multiple_values(val, n-1, lst)
res = add_multiple_values(3, 50, [])
print("The filtered list is:", res)
#this code contributed by tvsk
OutputThe filtered list is: [3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3]
The time complexity of this algorithm is O(n), since the function is called recursively n times and each call takes constant time. The append() method takes O(1) time to add an element to the end of a list, and the decrement operation and function call also take constant time.
The auxiliary space complexity of this algorithm is O(n), since the size of the call stack grows linearly with the number of recursive calls. However, in practice, the space complexity is unlikely to be a limiting factor, as the maximum recursion depth is typically quite large. The space complexity is also affected by the size of the list lst, but this is not directly related to the recursion and is dependent on the size of the input parameters.
Method #5 : Using operator.mul() method
Approach
- Repeat a list using operator.mul() method which returns a list
- Display list
Python3
# Python3 code to demonstrate
# to add multiple similar values
# adds 3, 50 times.
import operator
res = operator.mul([3],50)
# printing result
print ("The filtered list is : " + str(res))
OutputThe filtered list is : [3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3]
Time Complexity : O(N)
Auxiliary Space : O(N)
Similar Reads
Python - Convert Lists into Similar key value lists Given two lists, one of key and other values, convert it to dictionary with list values, if keys map to different values on basis of index, add in its value list. Input : test_list1 = [5, 6, 6, 6], test_list2 = [8, 3, 2, 9] Output : {5: [8], 6: [3, 2, 9]} Explanation : Elements with index 6 in corre
12 min read
Python - Append Similar Values as Key Sometimes, while working with data, we can have problems in which we need to categorize a particular list and values to similar keys. This can be a problem in counting data. Like counting votes or counting coins. Let's discuss certain ways in which this task can be performed. Method #1: Using loop T
7 min read
Append Multiple items to List - Python Appending items to a list in Python is an essential and common operation when working with different data structures. Sometimes, we need to add more than one item to a list at a time and in this article, we will explore the various ways to append multiple items to a list at the same time.Using exten
2 min read
Python - Group Similar items to Dictionary Values List We are given a list of items and our task is to group similar elements together as dictionary values. The keys will be the unique items and their values will be lists containing all occurrences of that item. For example, given ['apple', 'banana', 'apple', 'orange', 'banana'], the output should be: {
2 min read
Python - Index Value Summation List To access the elements of lists, there are various methods. But sometimes we may require to access the element along with the index on which it is found and compute its summation, and for that, we may need to employ different strategies. This article discusses some of those strategies. Method 1: Nai
4 min read
Python | Assign value to unique number in list We can assign all the numbers in a list a unique value that upon repetition it retains that value retained to it. This is a very common problem that is faced in web development when playing with id's. Let's discuss certain ways in which this problem can be solved. Method #1: Using enumerate() + list
7 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
How to Append Multiple Items to a List in Python Appending multiple items to a list in Python can be achieved using several methods, depending on whether you want to extend the list with individual elements or nested collections. Letâs explore the various approaches.Using extend method (Adding multiple items from iterable)The list.extend() method
2 min read
Add only Numeric Values Present in a List - Python To sum only the numeric values in a list containing mixed data types, we iterate through the list, filter out non-numeric elements and then calculate the sum of the remaining numeric values. Using filter and sumOne efficient to handle this is by combining the filter() function with the sum() functio
3 min read
Python | Adding value to sublists Sometimes, we just have to manipulate a list of lists by appending a similar value to all the sublists. Using a loop for achieving this particular task can be an option but sometimes leads to sacrificing the readability of code. It is always wanted to have a oneliner to perform this particular task.
6 min read