2

Is it possible to use slice but on a specific element on a list? For example a = [1,2,3,4,5,6,7,8,9] I want to make a for loop that prints out the whole list except the second element.

I want to make something like this:

for i in a[something_slice]: 
    print(i)

Is this possible?

Aplet123
  • 33,825
  • 1
  • 29
  • 55
Amx
  • 33
  • 1
  • 5
  • No, slices don't work like that, though it is easy enough to print all but the second element. This is one of the few areas where R is more flexible than Python. – John Coleman Nov 15 '20 at 12:51
  • Also [possible dupe on a different account](https://stackoverflow.com/q/64834210/5923139). – Aplet123 Nov 15 '20 at 12:55
  • @Aplet123 Seems like a homework assignment where OP wasn't happy with the first answer. On the other hand, this could be two students from the same class. – John Coleman Nov 15 '20 at 13:00

3 Answers3

1

For excluding just one element, the 2 slice lst[:i] + lst[i + 1:] approach proposed by @Applet123 is probably the fastest (Or perhaps a excluded = lst.pop(1) to extract the excluded element and for x in lst: print(x) for printing all the others; then lst.insert(1,excluded) to put the excluded element back on the list. See data structures docs for details).

If you just want to filter out certain indexes, instead of a for loop I recommend you use a more pythonic (and intuitive) approach based on list comprehensions and enumerate:

myList = [1,2,3,4,5,6,7,8,9]
excludedIndices = [1]
myFilteredList = [x for i, x in enumerate(myList) if i not in excludedIndices]
print (myFilteredList)

# output:
# [1,3,4,5,6,7,8,9]

# or, to actually print each element individually:
for x in myFilteredList:
  print (x)

# which can also work as a 2-liner with inline filtering:
for i, x in enumerate(myList):
  if i not in excludedIndices: print(x)

Also check out python usage of filter and map builtin functions, which may be overkill for this purpose but still offer a general and more powerful solution for this kind of processing:

# filters an enumerated element 
def myFilter(element): 
  return element[0] not in excludedIndices

# maps an enumerated element to a function 
def myMap(element): 
  print(element[1])
  
# runs myMap function for each enumerated element on the list filtered by myFilter
for x in map(myMap,filter(myFilter,enumerate(myList))): pass

Which you can also turn into a one-liner using lambda expressions:

for x in map(lambda x: print(x[1]),filter(lambda x: x[0] not in excludedIndices,enumerate(myList))): pass
NotGaeL
  • 8,344
  • 5
  • 40
  • 70
0

you can do it without slicing, using enumerate()

index_to_skip=1
for idx,item in enumerate(a):
    if idx!=index_to_skip:
        print(item)
Amit Kumar
  • 613
  • 3
  • 15
  • The problem is that my list contains attributes of a class. Your method works, but then I want to add on conditions so that it can print out what I need and take into account that I have skipped one element – Amx Nov 15 '20 at 12:59
0

If you actually want to slice the list, you can use 2 slices to slice around it:

def exclude(lst, i):
    return lst[:i] + lst[i + 1:]

exclude([1, 2, 3, 4, 5], 1) # [1, 3, 4, 5]

If you just want to loop through it, you could alternatively just skip when the index reaches the value you want to skip:

for i, v in enumerate(a):
    if i == 1:
        continue
    print(v)
Aplet123
  • 33,825
  • 1
  • 29
  • 55