FAQ: Introduction to Lists in Python - List Methods

This community-built FAQ covers the “List Methods” exercise from the lesson “Introduction to Lists in Python”.

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

Visualize Data with Python
Data Scientist
Analyze Data with Python
Computer Science
Analyze Financial Data with Python
Build Chatbots with Python
Build Python Web Apps with Flask
Data Analyst

Learn Python 3
CS101 Livestream Series

FAQs on the exercise List Methods

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

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

Ask or answer a question about this exercise by clicking reply (reply) below!
You can also find further discussion and get answers to your questions over in Language Help.

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

Need broader help or resources? Head to Language Help and Tips and Resources. If you are wanting feedback or inspiration for a project, check out Projects.

Looking for motivation to keep learning? Join our wider discussions in Community

Learn more about how to use this guide.

Found a bug? Report it online, or post in Bug Reporting

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!

Hi! have this question with the code:

example_list = [1, 2, 3, 4]

#Using Append

example_list.append(True)

print(example_list)

[1, 2, 3, 4, True]

#Using Remove

example_list.remove(True)

print(example_list)

[2, 3, 4, True]

this removes the first item from the list, but not the boolean

Hi @feliperivas598962492!
Good question, did you find the answer?
I also wondering the reason why it does not remove the True and how it works with boolean.

Eww, that’s horribly deceptive, at least you’re aware of it now. @feliperivas598962492 It’s due to the fact that statements like 1 == True == 1.0 evaluate to True in Python. I’d only really heard of it in terms of dictionary keys and sets where similar things are a pain (they have the same hash and no collision) but I suppose it holds true elsewhere.

1 Like

@tgrtim
Thanks for your reply.

1 Like

thanks for your reply! @tgrtim
Now I understand, but what if I needed to remove that argument, how do I identify it? if 1 == True == 1.0 and in the list I have both?

1 Like

Hmm, the easiest might be to remove it by index, e.g. del example_list[-1] or example_list.pop(index=-1). If you’re looking for a more generic solution where you don’t already know the index the only thing that springs to mind is checking the type of each object and only removing the boolean.

Something like the following could help you find it, you could extend this out with enumerate and a function or something to get the index and work from there.

for elem in example_list:
    # some basic type checking
    print(isinstance(elem, bool))
    print(type(elem) == bool)

Perhaps there’s a cleaner option but it’s not springing to mind at the moment. A good reason to try and limit lists to a single data type where possible I suppose.

1 Like

now I understand, thank you very much!.

1 Like

how we can append multiple items in a same line of .append( ) function, without creating a new line of .append( ) for adding multiple items in the list

Append adds a single item, .extend can add multiple items when using an iterable argument as can the augmented assignment operator +=.
https://docs.python.org/3/tutorial/datastructures.html#more-on-lists

lst_a = [1, 2, 3,] lst_b = [4, 5, 6,] print(lst_a) lst_a.extend(lst_b) # lst_b is iterable (a list) print(lst_a) lst_a += lst_a # as is lst_a itself print(lst_a)
1 Like

thanks @tgrtim youre best