Python - Repeat and Multiply List Extension


Python is a very commonly used programming language used for many different purposes such as Web Development, Data Science, Machine Learning and to perform many different processes with automation. One of the common processes followed by programmers is to make changes in the data of the list or add some more data into already existing list. In this article we will learn how to repeat and multiply list extension.

Repeating Elements

In this method the data in the list is extended by repeating the same previous data present in the list. The different methods to repeat the data in the list are as follows:

List Comprehension

We will use the list comprehension, which will check each element in the list and repeat as many times as we want. The syntax of this method is as follows:

def repeat_data_in_list(data, value):  # In the function repeat_data_in_list we will provide different parameters.
    # Data will represent the list from which data is to be taken and no. will represent the number of times the data is to be repeated
    new_list = [item for item in data for _ in range(value)]  # Each element of the list is checked to repeat the data
    return new_list 

Example

The example of the above method is as follows:

def repeat_data_in_list(data, value):  # In the function repeat_data_in_list we will provide different parameters.
    # Data will represent the list from which data is to be taken and no. will represent the number of times the data is to be repeated
    new_list = [item for item in data for _ in range(value)]  # Each element of the list is checked to repeat the data
    return new_list 
names = ['John', 'Sam', 'Daniel']  #The input to be taken is defined
new_name_list = repeat_data_in_list(names, 3)  #We provide the argument to define the input and the number of time data is to be repeated
print(new_name_list)  # The new repeated data is to be printed

Output

The output of the above example will be as follows:

['John', 'John', 'John', 'Sam', 'Sam', 'Sam', 'Daniel', 'Daniel', 'Daniel'] 

Usage of operator

This operator is generally used to represent the multiplication symbol. In this method also we will multiply the data with the number of times it is to be repeated. The syntax of the above method will be as follows:

def repeat_data_in_list(data, value):  # The data of the list and the number of times it is to be repeated is given as input to the function repeat_data_in_list
    new_list = data * value # It gives the information about the number of times the data is to be repeated by multiplication
    return new_list

Example

The example of the above code is as follows:

def repeat_data_in_list(data, value):  # The data of the list and the number of times it is to be repeated is given as input to the function repeat_data_in_list
    new_list = data * value # It gives the information about the number of times the data is to be repeated by multiplication
    return new_list
Countries = ["India", "Germany", "Israel", "Canada"] # The input is given
new_name_list = repeat_data_in_list(Countries, 3)  #The argument is provided of how many times to repeat the data
print(new_name_list)  #Print the new repeated list

Output

The output of the above code will be as follows:

['India', 'Germany', 'Israel', 'Canada', 'India', 'Germany', 'Israel', 'Canada', 'India', 'Germany', 'Israel', 'Canada'] 

Using Itertools

Itertools are used to create iterators for proper looping of data. We will use the repeat function of itertool to repeat the data. The syntax of this method is as follows:

import itertools # Do not forget to import itertools or else error might occur

def repeat_data_in_list(data, value):  #The data in the list and the number of time it is to be repeated is given as the input
    new_list = list(itertools.repeat(data, value)) # We will then use the itertools repeat to repeat the data required number of times
    return new_list 

Example

The example of the above method is as follows:

import itertools # Do not forget to import itertools or else error might occur

def repeat_data_in_list(data, value):  #The data in the list and the number of time it is to be repeated is given as the input
    new_list = list(itertools.repeat(data, value)) # We will then use the itertools repeat to repeat the data required number of times
    return new_list 
Countries = ["India", "Germany", "Israel", "Canada"] # The input is given
new_name_list = repeat_data_in_list(Countries, 3) # The argument is given to take countries as the input and the number of times it is to be repeated
print(new_name_list) 

Output

The output of the above example will be as follows:

['India', 'Germany', 'Israel', 'Canada', 'India', 'Germany', 'Israel', 'Canada', 'India', 'Germany', 'Israel', 'Canada'] 

Multiplying Elements

This method then repeating the data in the list. In this method the content in the list is multiplied as per need. The different methods to multiply elements are:

List Comprehension

In this method we will check each element in the list and multiply it by a factor defined. The syntax of the above method will be as follows:

def multiply_data_in_list(data, values):  # The input of data of the list and the factor(no.) is given into the function
    new_list = [item * values for item in data] # The list comprehension will check each element in the list and multiply it with the factor provided
    return new_list

Example

The example of the method is as follows:

def multiply_data_in_list(data, values):  # The input of data of the list and the factor(no.) is given into the function
    new_list = [item * values for item in data] # The list comprehension will check each element in the list and multiply it with the factor provided
    return new_list
Names = ['John', 'Sam', 'Daniel', 'Jack']  # The input of name is given into the program
new_names = multiply_data_in_list(Names, 3)  #The argument is provided to multiply it three times
print(new_names)  # The output after printing will be visible

The output of the above example will be as follows:

['JohnJohnJohn', 'SamSamSam', 'DanielDanielDaniel', 'JackJackJack']

Numpy

Python's NumPy library is a strong tool for numerical computation. It offers a number of array manipulation operations, including the capacity to elementally multiply arrays. This method is mainly useful for numerical values. The syntax of the above method is as follows:

import numpy as np  #Do not forget to import numpy or else error might occur

def multiply_data_in_list(data, values):  # The input of the data in the list and number of times to be repeated is as follows
    new_list = np.array(data) * values  # The data is converted into array and then the array is multiplied the number of times 
    return new_list.tolist()  # Once the multiplication is done, array is again converted into list

Example

The example of the above method is as follows:

import numpy as np  #Do not forget to import numpy or else error might occur

def multiply_data_in_list(data, values):  # The input of the data in the list and number of times to be repeated is as follows
    new_list = np.array(data) * values  # The data is converted into array and then the array is multiplied the number of times 
    return new_list.tolist()  # Once the multiplication is done, array is again converted into list
#The example of the above method is as follows: -
Names = [1, 2, 3, 4, 5, 6]  # The input of name is given into the program
new_names = multiply_data_in_list(Names, 3) #The argument is provided to multiply the array three times
print(new_names)  # The output after printing will be visible

Output

The output of the above example will be as follows:

[3, 6, 9, 12, 15, 18] 

Conclusion

The process of editing and adding the data in the list is a very common process done by python programmers and thus it is very necessary for a programmer to have knowledge about the different methods. Having knowledge about all these different methods will help the programmer to become efficient and he/she will be able to know, when to use which method. The above articles describe many different methods to repeat and multiply the data in a list

Updated on: 01-Aug-2023

39 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements