Creating a list of numbers and displaying elements while stripping first element

This blog contain program that creates a number list. Then uses while loop to display elements in a list and at the same time stripping first element at each loop interval

Create a number list

num = [1,2,3,4,5,6]

Using while loop to display elements and stripping first element using [1:]

while num:
print(num)
num = num[1:]

Output

[1, 2, 3, 4, 5, 6]
[2, 3, 4, 5, 6]
[3, 4, 5, 6]
[4, 5, 6]
[5, 6]
[6]

Complete Code

num = [1,2,3,4,5,6]
while num:
    print(num)
    num = num[1:]
Advertisement

Leave a Comment

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s