Home > front end >  skip first and last item in the list in python?
skip first and last item in the list in python?

Time:06-25

iplist = ['1.1.1.1', '2.2.2.2', '3.3.3.3', '4.4.4.4', '5.5.5.5']

How do i skip first ,last entry and run a for loop in items between ? I am aware of skipping first([1:]) and last([:-1]) separate but not together.

CodePudding user response:

You can use -ve slicing -> Skip 1st element & last element

iplist[1:-1]

For loop will look like this:

for i in iplist[1:-1]:
    print(i)

Output will be:

2.2.2.2 
3.3.3.3
4.4.4.4

CodePudding user response:

Got the answer:

for ip in iplist[1:][:-1]: #Do stuff

  • Related