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)
3 Answers 3
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)
-
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 incharts
), presence of9
in thediscard_idx_list
would not cause any problem. Kudos!– CypherXCommented Sep 6, 2019 at 1:40
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:
- a list of indices: [0, 2, 4, ]
- 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)
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)
-
You can use
operator.itemgetter
to extract multiple elements from a list at one time.– GZ0Commented Sep 6, 2019 at 0:55 -
1You could do
excluded = set([chart[x] for x in (0,2,4)])
, which concise and more pythonic.– CypherXCommented Sep 6, 2019 at 1:36 -
-
Explore related questions
See similar questions with these tags.
for x in chart[1::2]:
, which means "for each item in chart, starting with the second item, and every second item thereafter".any
for this.if any(x==y for y in chart[::2]): continue
.