GeeksforGeeks App
Open App
Browser
Continue

Python | Add similar value multiple times in list

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:

1. Define a function add_multiple_values(val, n, lst) that takes a value val, an integer n, and a list lst.\
2. If n is equal to 0, return lst.
3. Append val to the end of the list lst using the append() method.
4. Decrement n by 1.
6. 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`

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]`

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

1. Repeat a list  using operator.mul() method which returns a list
2. 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))`

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)

Auxiliary Space : O(N)

My Personal Notes arrow_drop_up