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 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 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]
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 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]
Recommended Posts:
- Python | Sorting list of lists with similar list elements
- Python | Grouping similar substrings in list
- Python | Checking if starting digits are similar in list
- Python | Repeat each element K times in list
- Remove multiple elements from a list in Python
- Python | Remove elements of list that are repeated less than k times
- Python | Check if any element occurs n times in given list
- Python | Check if k occurs atleast n times in a list
- Python | Assign multiple variables with list values
- Python | Convert a list of multiple integers into a single integer
- Get similar words suggestion using Enchant in Python
- Python | os.times() method
- Calculate n + nn + nnn + ... + n(m times) in Python
- Rename multiple files using Python
- Python | Replace multiple characters at once
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.



