Hi guys, in this article you will learn a Python program to create a Python list that contains infinite elements. if we try to say an infinite element so we will be using an infinite while loop to add the elements to the list.
Let's try to solve the python program.
In order to solve this kind of program you have to take infinite random values as elements to the Python list and these random elements to the list.
Let's use the random module in order to generate the Random numbers infinitely and keep appending these elements to the list.
Write A Python Program To Create A List With Infinite Elements
Let's import the required module, i.e, we require a random module in order to generate the Random numbers to append to the list.
Now take a list variable and initialize it to empty list, This list will be holding The Infinite elements that we keep adding to the list
import random
mylist = []
Use the infinite while loop, that is while loop with the true statement will give us the infinite while loop. now inside the while loop use the append built in function to append the random integer values.
To generate the random integer values between 1 and 500 just use randint() function call. The while loop will keep repeating indefinitely by adding random integers to the list.
This is the process of generating random integer elements infinitely and having elements appended to the list.
while True:
mylist.append(random.randint(1, 500))
Complete Python program
==============================
import random
mylist = []
while True:
mylist.append(random.randint(1, 500))
Output
=============
The above Python program does not give any output as it keeps adding the elements in finitely to the list
Note: This program should not be run on the personal computer, it should run on the machine which has huge computing power. This program may also crash Your hard disc drive so be safe while running this Python program.
The above python program creates a process, where it creates a list containing infinite elements. The Python list will be stored in the main memory (RAM) once the main memory is field it starts to keep writing into the hard disc drive.
so if the hard disc drive is full then you start getting errors as this program generates The Infinite Python list elements and keeps adding to the list.
Conclusion
===================
In the above Python program I have used a random module to generate the random elements of a list, an infinite loop to keep generating the random integers and keep appending it to the list.
Execute the above Python program and the program does not give you any result. it gives adding the list element to the list infinitely.
Comment it down below you have any queries regarding the above python program
0 Comments