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)
asked Sep 6, 2019 at 0:14
4
  • 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 Commented Sep 6, 2019 at 0: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". Commented Sep 6, 2019 at 0: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! Commented Sep 6, 2019 at 0:36
  • You can also use any for this. if any(x==y for y in chart[::2]): continue.
    Henry Yik
    Commented Sep 6, 2019 at 2:30

3 Answers 3

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)
answered Sep 6, 2019 at 0:20
1
  • 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
    Commented Sep 6, 2019 at 1:40
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)
answered Sep 6, 2019 at 2:14
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)
answered Sep 6, 2019 at 0:51
4
  • You can use operator.itemgetter to extract multiple elements from a list at one time.
    GZ0
    Commented Sep 6, 2019 at 0:55
  • 1
    You could do excluded = set([chart[x] for x in (0,2,4)]), which concise and more pythonic.
    CypherX
    Commented Sep 6, 2019 at 1:36
  • @CypherX, nice!
    Javier
    Commented Sep 6, 2019 at 20:18
  • @GZ0, I don't like how it looks.
    Javier
    Commented Sep 6, 2019 at 20:20

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.