Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python

I need a full explanation of Lists, and its commands similar to .append(), .extend(), pop() and the others.

I watched all the videos related to the lists and its functions, i cannot really understand any of the commands of lists like append, pop, extend, remove and the other stuff, If anyone could explain them in a a good manner without much details and with examples, Would be great! Thanks!

1 Answer

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

I'll give it my best shot! :smiley:

Append: Add an element to the end of a list.

fruits = ['banana', 'apple', 'pear', 'orange']
fruits.append('kiwi')

Our fruits list now looks like this: ['banana', 'apple', 'pear', 'orange', 'kiwi']

Delete (del): Remove an item from a list.

fruits = ['banana', 'apple', 'pear', 'orange']
del fruits[0];

Our fruits list now looks like this: ['apple', 'pear', 'orange']

Insert: Inserts an item into the list at a given index

fruits = ['banana', 'apple', 'pear', 'orange']
fruits.insert(1, 'kiwi')

Our fruits list now looks like this: ['banana', 'kiwi', 'apple', 'pear', 'orange']

Pop: Remove an item from a list at a given index (or the end if no index is given) :information_source: The primary difference between this and del is that the value that is removed is returned

fruits = fruits = ['banana', 'apple', 'pear', 'orange']
x = fruits.pop(1);

Our fruits list now looks like this: ['banana', 'pear', 'orange'] and x is now equal to 'apple'

Extends: Also adds items to a list. :information_source: The primary difference between this and append is that you can concatenate lists

fruits = ['banana', 'apple', 'pear', 'orange']
fruits2 = ['kiwi', 'lemon']
fruits.extend(fruits2)

Our fruits list now looks like this: ['banana', 'apple', 'pear', 'orange', 'kiwi', 'lemon']

I hope this helps! Let me know if I've missed any that you need explained. :sparkles:

Thank you so much! This is really great and very helpful!

Thank you so much! But can you please tell em how do i extend a list to add numbers from a number to another number? i mean the output would be like "1, 2, 3, 4, 5, 6, 7, 8, 9, 10", Thanks!

Jennifer Nordell
seal-mask
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Mina Basem I'm sorry, I don't exactly understand what you mean. Can you give me a more concrete example?

Sorry for your misunderstanding but i am actually doing the "Manipulating Lists" Quiz but i am not able to do the 3rd Task. Which is supposed to be extending the list, but i am not able to do it.

I am sorry, i just figured it out, using "the_list.extend(range(4,21))"

Thank you for the help! :)