2

This a question on coursera practices excess.

Create a new list using the 9th through 12th elements (four items in all) of new_lst and assign it to the variable sub_lst.

link to the question: https://runestone.academy/runestone/books/published/fopp/Sequences/TheSliceOperator.html The last question on the list.

How is my code

new_lst = ["computer", "luxurious", "basket", "crime", 0, 2.49, "institution", "slice", "sun", 
["water", "air", "fire", "earth"], "games", 2.7, "code", "java", ["birthday", "celebration", 1817, 
"party", "cake", 5], "rain", "thunderstorm", "top down"]
new_lst = new_lst[9:12]
sub_lst = new_lst
print(sub_lst)

My output:

[['water', 'air', 'fire', 'earth'], 'games', 2.7]

But here is the expected output:

['sun', ['water', 'air', 'fire', 'earth'], 'games', 2.7]

Please why am I not getting the expected output?

2
  • Please add a link to original question here. Commented Feb 18, 2020 at 5:26
  • Possibly because you destroyed new_lst in the process? You should have assigned the slice directly to sub_lst instead. Commented Feb 18, 2020 at 5:30

5 Answers 5

1

You need to start indexing at 8 as The 9th element is at index 8.

new_lst = ["computer", "luxurious", "basket", "crime", 0, 2.49, "institution", "slice", "sun", ["water", "air", "fire", "earth"], "games", 2.7, "code", "java", ["birthday", "celebration", 1817, "party", "cake", 5], "rain", "thunderstorm", "top down"]

sub_lst=new_lst[8:8+4]

Resulting output will be -

['sun', ['water', 'air', 'fire', 'earth'], 'games', 2.7]
Sign up to request clarification or add additional context in comments.

Comments

0

I think you are assigning more than the question asked and here is the solution

new_lst = ["computer", "luxurious", "basket", "crime", 0, 2.49, "institution", "slice", "sun", ["water", "air", "fire", "earth"], "games", 2.7, "code", "java", ["birthday", "celebration", 1817, "party", "cake", 5], "rain", "thunderstorm", "top down"]
sub_lst = new_lst[8:12]

just do "sub_lst = new_lst[8:12]" assign just sliced list in sub_lst.

Comments

0

your 9th element is ['water', 'air', 'fire', 'earth'] (it is included) and 12th element is "code" (it is excluded in python) so result is [['water', 'air', 'fire', 'earth'], 'games', 2.7]

2 Comments

Yes The result is [['water', 'air', 'fire', 'earth'], 'games', 2.7] But from the online textbook the The Expected Result [ ] runestone.academy/runestone/books/published/fopp/Sequences/…
At that book expected answer is not empty array your answer is new_lst = ["computer", "luxurious", "basket", "crime", 0, 2.49, "institution", "slice", "sun", ["water", "air", "fire", "earth"], "games", 2.7, "code", "java", ["birthday", "celebration", 1817, "party", "cake", 5], "rain", "thunderstorm", "top down"] sub_lst=new_lst[8:12]
0

Here is the code I executed in the link you have provided:

new_lst = ["computer", "luxurious", "basket", "crime", 0, 2.49, "institution", "slice", "sun", ["water", "air", "fire", "earth"], "games", 2.7, "code", "java", ["birthday", "celebration", 1817, "party", "cake", 5], "rain", "thunderstorm", "top down"]
sub_lst = new_lst[8:12]
print(sub_lst)

This line in the code you have written, you have updated the value of new_lst to only 4 elements.

new_lst = new_lst[9:12]

So, it is showing the expected value as [].

Comments

0

If you do a simple output of the 9th through 12th items of the list

item_count = 0
for item in new_lst:
    item_count += 1
    if 9<=item_count<=12:
        print(str(item_count)+" "+str(item))

you get the output

9 sun
10 ['water', 'air', 'fire', 'earth']
11 games
12 2.7

which is not an empty list.

To get the sub-list of the 9th through 12th items use a slice that starts at 8 based on a zero-based index and ends at 12 because the last item is not included in the output slice.

sub_lst = new_lst[8:12]

which prints as

print(sub_lst)
['sun', ['water', 'air', 'fire', 'earth'], 'games', 2.7]

Comments

Your Answer

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.