Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
list - 2 questions:
#34
And don't forget that the function has to return the string "hydrogen, lithium and boron". Once you have the list of items ["hydrogen", "lithium", "boron"] you still need to compose the string.

Writing things down with pencil and paper does not always directly translate to code, but it is a good step for understanding the problem. My first pass would look like this:

items = ["hydrogen", "helium", "lithium", "beryllium", "boron", "magnesium"]

Then I would ask myself "How do I remove the odd items in the list?" This is rather difficult because when you remove the first odd item how do you remember which was the next odd item?

items = ["hydrogen", "lithium", "beryllium", "boron", "magnesium"] Which is next odd??

After pondering that for a while I would probably look back at the original problem and try a different approach:

items = ["hydrogen", "helium", "lithium", "beryllium", "boron", "magnesium"]

Selecting even items is easier that removing odd items. I want items 0, 2, 4, 6, 8... Notice the index starts at zero and increases by 2. I want to stop when I run out of items.
even_items = []
i = 0
while i < len(items)-1:
    even_items.append(items[i])
    i := i + 2
I've noticed that there are a lot of for loops in Python examples and not that many while loops. I wonder if I can use a for loop? I remember seeing for loops using "range()" a lot. What does range() do?

https://docs.python.org/3/library/stdtypes.html#range
Quote:class range(start, stop[, step])
The arguments to the range constructor must be integers (either built-in int or any object that implements the __index__ special method). If the step argument is omitted, it defaults to 1. If the start argument is omitted, it defaults to 0. If step is zero, ValueError is raised.

For a positive step, the contents of a range r are determined by the formula r[i] = start + step*i where i >= 0 and r[i] < stop.
even_items = []
for i in range(0, len(items), 2):
    even_items.append(items[i])
I might use that solution, or I might do some more investigation. I could use a list comprehension:

https://docs.python.org/3/tutorial/datas...prehension
even_items = [items[i] for i in range(0, len(items), 2)]
Or I might use a slice:

https://docs.python.org/3/library/functi...lice#slice
even_items = items[0:len(items):2]
And reading a bit more I would see that the 0 and len(items) can be left out.
even_items = items[::2]
Now use the same approach to converting the even_items list to a string. Start with writing down the solution on a piece of paper.

even_items[0] + ', ' + even_items[1] + ' and ' + even_items[2]

How can you make a generic solution for lists of varying lengths? What would it be for a list of length 2? length 5? length 1?
ben1122 likes this post
Reply


Messages In This Thread
list - 2 questions: - by ben1122 - Jul-31-2021, 11:33 AM
RE: list - 2 questions: - by ndc85430 - Jul-31-2021, 11:36 AM
RE: list - 2 questions: - by ben1122 - Jul-31-2021, 11:41 AM
RE: list - 2 questions: - by ben1122 - Jul-31-2021, 11:43 AM
RE: list - 2 questions: - by ndc85430 - Jul-31-2021, 11:46 AM
RE: list - 2 questions: - by ben1122 - Jul-31-2021, 11:48 AM
RE: list - 2 questions: - by ben1122 - Jul-31-2021, 11:44 AM
RE: list - 2 questions: - by ndc85430 - Jul-31-2021, 11:45 AM
RE: list - 2 questions: - by ben1122 - Jul-31-2021, 11:47 AM
RE: list - 2 questions: - by ndc85430 - Jul-31-2021, 12:05 PM
RE: list - 2 questions: - by ben1122 - Jul-31-2021, 12:08 PM
RE: list - 2 questions: - by ndc85430 - Jul-31-2021, 12:10 PM
RE: list - 2 questions: - by ben1122 - Jul-31-2021, 12:16 PM
RE: list - 2 questions: - by ben1122 - Jul-31-2021, 12:17 PM
RE: list - 2 questions: - by ben1122 - Jul-31-2021, 12:17 PM
RE: list - 2 questions: - by ben1122 - Jul-31-2021, 12:19 PM
RE: list - 2 questions: - by ndc85430 - Jul-31-2021, 12:23 PM
RE: list - 2 questions: - by ben1122 - Jul-31-2021, 12:31 PM
RE: list - 2 questions: - by ndc85430 - Jul-31-2021, 12:37 PM
RE: list - 2 questions: - by ben1122 - Jul-31-2021, 12:39 PM
RE: list - 2 questions: - by ben1122 - Jul-31-2021, 12:37 PM
RE: list - 2 questions: - by ben1122 - Jul-31-2021, 12:40 PM
RE: list - 2 questions: - by ben1122 - Jul-31-2021, 12:49 PM
RE: list - 2 questions: - by ndc85430 - Jul-31-2021, 12:50 PM
RE: list - 2 questions: - by ben1122 - Jul-31-2021, 12:53 PM
RE: list - 2 questions: - by ndc85430 - Jul-31-2021, 01:00 PM
RE: list - 2 questions: - by ben1122 - Jul-31-2021, 01:03 PM
RE: list - 2 questions: - by ndc85430 - Jul-31-2021, 01:24 PM
RE: list - 2 questions: - by ben1122 - Jul-31-2021, 01:32 PM
RE: list - 2 questions: - by ndc85430 - Jul-31-2021, 01:36 PM
RE: list - 2 questions: - by ben1122 - Jul-31-2021, 02:03 PM
RE: list - 2 questions: - by ndc85430 - Jul-31-2021, 02:05 PM
RE: list - 2 questions: - by ben1122 - Jul-31-2021, 02:24 PM
RE: list - 2 questions: - by deanhystad - Jul-31-2021, 03:11 PM
RE: list - 2 questions: - by ben1122 - Jul-31-2021, 03:31 PM
RE: list - 2 questions: - by deanhystad - Jul-31-2021, 05:06 PM
RE: list - 2 questions: - by Pedroski55 - Jul-31-2021, 11:18 PM
RE: list - 2 questions: - by Pedroski55 - Jul-31-2021, 11:48 PM
RE: list - 2 questions: - by Pedroski55 - Aug-01-2021, 03:06 AM
RE: list - 2 questions: - by snippsat - Aug-01-2021, 04:27 PM
RE: list - 2 questions: - by naughtyCat - Aug-18-2021, 05:57 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Discord bot that asks questions and based on response answers or asks more questions absinthium 1 43,542 Nov-25-2017, 06:21 AM
Last Post: heiner55

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020