Predictive Hacks

How to repeat each list element n times in Python

Let’s say that we have the following list [1,2,3,4,5] and we want to repeat each element 3 times. Let’s see how we can do it in Python and numpy.

import numpy as np

x = [1,2,3,4,5]

# repeat each element 3 times

And we get:

array([1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4, 5, 5, 5])

Share This Post

Share on facebook
Share on linkedin
Share on twitter
Share on email

Subscribe To Our Newsletter

Get updates and learn from the best

More To Explore


Image Captioning with HuggingFace

Image captioning with AI is a fascinating application of artificial intelligence (AI) that involves generating textual descriptions for images automatically.


Intro to Chatbots with HuggingFace

In this tutorial, we will show you how to use the Transformers library from HuggingFace to build chatbot pipelines. Let’s