1

I have a list where I know that some items are unnecessary to print and I'm trying to do that through if statement... but it's getting very convoluted so is there any way to include multiple indexes in the if statement without rewriting whole statement. Something that would look like like this?: if x == chart[0,2,4]

Example that I have:

chart = ['milk', 'soda', 'cookies', 'yogurt', 'rug']

for x in chart:
    if x == chart[0] or x == chart[2] or x == chart[4]:
        continue
    else:
        print(x)
  • No, I am just trying to get the code cleaner and don't want to repeat the same stuff over and over again... all I need to do is to avoid some items from the list. In this case 'milk', 'cookies' and 'rug' .... and print everything else – inarisound inarisound Sep 06 '19 at 00:22
  • 1
    For this specific example you could use `for x in chart[1::2]:`, which means "for each item in chart, starting with the second item, and every second item thereafter". – John Gordon Sep 06 '19 at 00:30
  • @JohnGordon thank you very much it's a great solution! But I was trying to get indexes that has no pattern so Barmar 's solution would do exactly what I was looking for. Thank you once again! – inarisound inarisound Sep 06 '19 at 00:36
  • You can also use `any` for this. `if any(x==y for y in chart[::2]): continue`. – Henry Yik Sep 06 '19 at 02:30

3 Answers3

2

Use enumerate() so you get the indexes, then you can just check that.

for i, x in enumerate(chart):
    if i not in {0, 2, 4}:
        print(x)
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • Your solution will also work if the list of indices was like: `discard_idx_list = (0, 2, 4, 9)`. Even though there is no index beyond 4 (only 5 items are in `charts`), presence of `9` in the `discard_idx_list` would not cause any problem. Kudos! – CypherX Sep 06 '19 at 01:40
0

Another one:

chart = ['milk', 'soda', 'cookies', 'yogurt', 'rug']

excluded = {chart[i] for i in (0, 2, 4)}
for x in chart:
    if x not in excluded:
        print(x)
Javier
  • 2,752
  • 15
  • 30
0

Although I support what @Barmar suggested, I consider the solution has a loop hole for the scenario when charts has non-unique entries in it. Such as,

charts = ['milk', 'soda', 'milk', 'cookies', 'yogurt', 'rug']

The following piece of code will address non-unique entries for charts as well as deliver what the question asks for.

discard_index = [0, 2, 4, 9]
# Keep only unique items in discard_items list
discard_items = set([chart[x] for x in discard_index if x<len(chart)]) 
for x in enumerate(chart):
    if x not in discard_items:
        print(x)

Detailed Discussion

You may want to select or drop elements based on:

  1. a list of indices: [0, 2, 4, ]
  2. a list of items: ['cookies', 'milk', 'rug', ]

If it is with a list of items, you may provide a list of items to discard wherein some items may not even be a part of charts. A conditional statement which checks the validity based on the indices, requires you to know the indices before hand.

So, what if you could process the for loop irrespective of what is provided: list of indices or list of items.
+ user_discard_items = ['cookies', 'milk', 'rug', 'bananas']
+ user_discard_index = [0,2,4,9]

Note that not all items (bananas) and all indices (9) are in the list, chart.

chart = ['milk', 'soda', 'cookies', 'yogurt', 'rug']
user_discard_items = ['cookies', 'milk', 'rug', 'bananas']
user_discard_index = [0,2,4,9]

If you want to discard based on a list of indices: user_discard_index, the solution would be what @Barmar suggested.

discard_index = user_discard_index.copy() 
for i, x in enumerate(chart):
    if i not in discard_index:
        print(x)

However, this could cause problem if charts had non-unique items, such as:

charts = ['milk', 'soda', 'milk', 'cookies', 'yogurt', 'rug']

In that case, if you are more interested in dropping milk, specifying discard_index = (0, 2, 4) will miss the second occurrence of milk. However, if you make a list of unique items to drop, that will address this issue. The following piece of code handles this scenario in addition to what @Barmar suggested.

# Keep unique items for the list: discard_index
discard_index = user_discard_index.copy()
discard_items = set([chart[x] for x in discard_index if x<len(chart)])
for x in enumerate(chart):
    if x not in discard_items:
        print(x)

Note that, the code above also works if the user provided a list of items (instead of indices) to drop. See the code block below.

# Keep unique items for the list: discard_items
discard_items = user_discard_items.copy()
discard_items = set(discard_items)
for x in enumerate(chart):
    if x not in discard_items:
        print(x)

CypherX
  • 7,019
  • 3
  • 25
  • 37