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.

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


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.

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


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

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


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]



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.