FAQ: Working with Lists in Python - Review

This community-built FAQ covers the “Review” exercise from the lesson “Working with Lists in Python”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Computer Science

FAQs on the exercise Review

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

5 posts were split to a new topic: Why doesn’t first 3 items in a list need [:2] to select?

How do I “Remove” items from a list, it’s never mentioned anywhere.

Have you checked the documentation for what built-ins and methods are available?

Is there an easy to way to remember when to use square brackets and when to use parenthesis? It seems arbitrary to me.

Square brackets have three different uses/meanings.

[]     =>  list object literal

[0]    =>  list, str, or set subscript

['a']  =>  dict object key subscript

Parens have a number of uses/meanings.

(a + b)    =>  grouping for precedence
foo(x)     =>  parameter list of a function
foo(7)     =>  argument list of a function call
(a, b, c)  =>  tuple literal

To round this out, let’s also look at curly braces…

{'key': 'value'}    =>  dict literal
{2, 5, 8, 11}       =>  set literal
1 Like

Thanks. I don’t know what a lot of those terms mean, though.

You will if you keep learning, which includes cycling back to review on a regular basis and using Google to look up terms.

dict literal py

If you tack on py to your search phrase, the SERP will contain more links appropriate to Python.

Can we enter any desired parameters in the parentheses for sort?

Can you take everything done in the review section and put it into a function?

A function should have some specific purpose and predictable outcomes given particular inputs. The review section is expecting several outcomes, each of which is a different slice. In a sense it is like a function if we consider slicing a type of method, and the indices we supply as arguments.

Hi- Quick question here. Why does the review of this lesson say:

" * Generate a list using the range() function."

The chapter is clear that range() returns a range object, that can be converted to a list with list(). This seems unnecessarily confusing as written.

If you use .pop() for checkpoints 2 and 3, it will give you the checkmark

However, when you do checkpoint 4 you will get an error even if you did it right.

I’d like to suggest removing the .pop() variation as a checkmark solution for checkpoints 2 and 3 because I was confused as to why my answer on checkpoint 4 was wrong.

1 Like

inventory_2_6=inventory[2:6] doubt in this line

but not including means

Hi there,

I’m currently going through the last exercise in Lists

When I do the following:

sort_inventory = inventory.sort()
print(sort_inventory)

It produces “None”.
Why is that?

Please note, when in contrast I command the following:

sorted_inventory = sorted(inventory)
print(“Sorted inventory:”,sorted_inventory)

then it prints the sorted list without issues.

sorted doesn’t change the original list, but returns a new sorted list.

.sort sorts the list in-place (i.e. the original list is modified), but returns None

x = [3, 4, 1, 2, 5, 1, 2]
y = sorted(x)
print(x) // [3, 4, 1, 2, 5, 1, 2]
print(y) // [1, 1, 2, 2, 3, 4, 5]

x = [3, 4, 1, 2, 5, 1, 2]
y = x.sort() 
print(x) // [1, 1, 2, 2, 3, 4, 5] 
print(y) // None

Useful Documentation:
sorted
sort
Sorting HOW TO

1 Like

use .pop
list.pop(1234)

In the review section of review for python lists I keep recieving a error on step 4:
inventory_2_6 = inventory[2:6]
Does anyone know a solution to this step ?

It’s difficult to say. Please post your formatted code.