In Python Data Structures, a list is a type of container that is used to store multiple pieces of data at the same time. In Python, unlike Sets, the list is ordered and has a definite count. A list’s elements are indexed in a specific order, and the indexing of a list begins with 0 as the first index.
This article will go over various methods to create a list and Initialize with same values.
Examples:
Input:
size=5 , value="BTechGeeks"
Output:
['BTechGeeks', 'BTechGeeks', 'BTechGeeks', 'BTechGeeks', 'BTechGeeks']
Make a list and fill it with the same values
There are several ways create a list and initialize with same values some of them are:
Method #1:Using [] and multiply operator
Assume we want to make a list of strings that contains 5 identical strings, such as ‘BTechGeeks’.
You can do the following to initialize a list of immutable items, such as None, strings, tuples, or frozensets, with the same value:
# Function which converts list to string def createList(size, value): # Using multiply operator requiredlist = [value]*size # return the list return requiredlist # Driver code # given value and size size = 5 value = "BTechGeeks" # passing value and size to createList function print(createList(size, value))
Output:
['BTechGeeks', 'BTechGeeks', 'BTechGeeks', 'BTechGeeks', 'BTechGeeks']
[‘BTechGeeks’] will generate a list with a single value, which we can then multiply by 5. It will loop through the contents of the list 5 times.
Note: Never use [e]*n for mutable items. As a result, the list will contain the same object e repeated N times, as well as referencing errors.
Method #2:Using List Comprehension and range()
List comprehension is a simple and compact syntax for creating a list from a string or another list in Python. It’s a quick way to make a new list by performing an operation on each item in the existing one. List comprehension is much faster than using the for loop to process a list.
The for loop in this list comprehension will iterate over the range object 5 times, adding ‘BTechGeeks’ to the list with each iteration.
Below is the implementation:
# Function which converts list to string def createList(size, value): # Using list comprehension and range requiredlist = [value for i in range(size)] # return the list return requiredlist # Driver code # given value and size size = 5 value = "BTechGeeks" # passing value and size to createList function print(createList(size, value))
Output:
['BTechGeeks', 'BTechGeeks', 'BTechGeeks', 'BTechGeeks', 'BTechGeeks']
Method #3: Using itertools repeat() function
The itertools package contains a repeat() function that creates an iterator that returns the object repeatedly. This has the same issue as [e]*n and should be avoided when dealing with mutable items.
Below is the implementation:
#importing itertools import itertools # Function which converts list to string def createList(size, value): # Using repeat() requiredlist = list(itertools.repeat(value, size)) # return the list return requiredlist # Driver code # given value and size size = 5 value = "BTechGeeks" # passing value and size to createList function print(createList(size, value))
Output:
['BTechGeeks', 'BTechGeeks', 'BTechGeeks', 'BTechGeeks', 'BTechGeeks']
Related Programs:
- how to create and initialize a list of lists in python
- python how to create a list of all the values in a dictionary
- python program to create a list of tuples with the first element as the number and second element as the square of the number
- how to code a scraping bot with selenium and python
- python how to create an empty list and append items to it
- python how to create a list of all the keys in the dictionary
- what is a structured numpy array and how to create and sort it in python