FAQ: Working with Lists in Python - Review

You have to use the print() function b/c the LE doesn’t automatically print the results.

Summary
...
last = inventory[-1]
print(last)

inventory_2_6 = inventory[2:6]
print(inventory_2_6)

As mentioned earlier in the thread,

The second and third steps mention:

Select the first element in inventory. Save it to a variable called first.

Select the last element from inventory. Save it to a variable called last.

In your screenshot, you are popping the elements. The instructions want you to only access the correct elements and assign them to the specified variables. The elements shouldn’t be removed from the inventory list. Step 7 onward involve removing and inserting elements from the inventory list. Earlier steps just want to access elements and slices without modifying the list.

2 Likes

How can we get the number of unique items in the inventory?

Have you learned about the other iterable objects in Python, namely, tuples and sets? They are similar to lists in that they are iterable, but they have differences, as well. A tuple is immutable, meaning we cannot change it. A set is unordered and has no duplicate members.

Say we have a list:

k = [1, 3, 5, 7, 9, 3, 5, 9, 1, 3, 5, 9, 7, 5]

What will the set look like if we cast it from this list?

>>> print (set(k))
{1, 3, 5, 7, 9}
>>>

The order may differ but the main thing to note is there are no duplicates.

2 Likes

I am confused by Checkpoint 5 - where it asks to select the first 3 items of inventory and to save it to a variable called first_3 .

Since indexes begin with “0”, why wouldn’t it be:
first_3 = inventory[0:2] ? (0, 1, 2) - that would be the first 3 indexes.

Instead, it says the correct answer is:
first_3 = inventory[0:3]

0, 1, 2 would call the first 3 items, right?
Why doesn’t [0:3] call the first 4 items? (0, 1, 2, 3,) ?

Recall that slicing behaves the same as the range() function.

list(range(10))  =>  [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9][0:3]  =>  [0, 1, 2]

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9][:3]  =>  [0, 1, 2]
1 Like

Thank you for the clarification. I’m afraid that I am going to probably continue to struggle with this concept and will rely on a trial and error type approach when it comes to future instances. I didn’t do well on the Quiz because of slicing. It’s just not “computing” to me.

1 Like

Perhaps think in terms of offset. That is how I did it way back when. We take our counting numbers, 1 to whatever, and offset them by negative 1 so that 3 becomes 2.

Now we can replace the word ‘exclusive’ with a new word, ‘offset’. If we want 2 as the rightmost value in our returned list, then we stipulate that by using its reverse offset, 3 in both range() and slicing operations. Eventually the two (while not synonymous) merge into one another aesthetically.

Also note in the above example, we use the reverse offset of 9, 10 in our range argument. How many returned elements are there? 10. Offsetting does not affect list (output) length.

a_list[:3]

will always be the first three items, regardless of their index, but it only holds if the start is zero (by default, hence it was left off).

As did I, but with active experimentation covering a wide spectrum of scenarios. If this concept is giving you any sort of difficulty, then best advice would be to pause your study and devote a day or two to just this concept in sandbox or on your machine. Feel free to bounce your ideas off us members, but don’t try to push on if this is a problem. You do need to solve this one right here and now, sorry to say.

As a reminder, in slices (and in ranges), the element at the stop index is not included.

letters = ["A", "B", "C", "D", "E", "F", "G", "H"]

print(letters[:5])
# ['A', 'B', 'C', 'D', 'E']
# The element at index 5 is "F"
# All elements before "F" are included

print(letters[4:6])
# ['E', 'F']
# Element at start index 4 is 'E' and is included in the slice.
# Element at stop index 6 is 'G' and is not included in the slice.
1 Like

I just went through the chapter again and did much better. I did get 1 wrong during the Quiz where I had to drag and drop, but much better than the first time around.

I just completed the “Len’s Slice” Project and the only point where I struggled was Checkpoint # 12 where I needed to add $2 sausage using the insert method. Turns out I was just missing a space, but it made me realize that I have a better grasp on indexes.

1 Like

Offsetting is a good way to put it and is how I have been able to understand it better. Thank you again for your help.

1 Like

@michaelmuschera hopefully the slicing logic is making a lot more sense now. I can see that it can be a novel and difficult concept to grasp.

Here’s another forum thread that´s dedicated to this discussion from others that felt the same:

Specifically, there was one post in there from someone else also new to coding. The person explained it wonderfully and visually in my opinion and is one that I always keep in mind:

Hope this helps!

using list slicing, to slice the list which includes the last element in the list it seems that you need to provide an index one beyond the last element. How does python handle this since that index doesn’t exist?

list[start:]

Python will capture the entire list to the right of and including start.

Note also that slicing is virtual so we will not raise any exceptions if we give non-existent indices.

>>> [1,2,3,4,5][1:6]
[2, 3, 4, 5]

Can you explain what’s meant by slicing is virtual?

Slices are not subject to exceptions since they exist only in memory, not in the namespace, proper. Since we are not referencing actual variables, we can describe the slice any way we wish. Python will attempt to build a list with the information we supply and never wince if the information is incorrect.

virtual … not physically existing as such but made by software to appear to do so.

Built-in Types — Python 3.12.0 documentation

I have a question on question 8.
There was a new item added to our inventory called "19th Century Bed Frame".
Use the .insert() method to place the new item as the 11th element in our inventory.

I have put inventory.insert(-1,“19th Century Bed Frame”)

but this is still wrong and i have tried others. Can you help me out here

did you try:

inventory.insert(10, "19th Century Bed Frame")

-1 won’t work b/c it places the item at the index before the one given.

See:

Alternatively, this would also work:

inventory.insert(len(inventory),"19th Century") #which is like inventory.append() 
2 Likes