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
Mina Basem
Courses Plus Student 1,071 PointsI 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
Treehouse TeacherI'll give it my best shot!
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)
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.
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.
Mina Basem
Courses Plus Student 1,071 PointsMina Basem
Courses Plus Student 1,071 PointsThank you so much! This is really great and very helpful!
Mina Basem
Courses Plus Student 1,071 PointsMina Basem
Courses Plus Student 1,071 PointsThank 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
Treehouse TeacherJennifer Nordell
Treehouse TeacherMina Basem I'm sorry, I don't exactly understand what you mean. Can you give me a more concrete example?
Mina Basem
Courses Plus Student 1,071 PointsMina Basem
Courses Plus Student 1,071 PointsSorry 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.
Mina Basem
Courses Plus Student 1,071 PointsMina Basem
Courses Plus Student 1,071 PointsI am sorry, i just figured it out, using "the_list.extend(range(4,21))"
Jennifer Nordell
Treehouse TeacherJennifer Nordell
Treehouse Teacher@Mina Basem can you link the challenge/quiz? I'll give it a look
Jennifer Nordell
Treehouse TeacherJennifer Nordell
Treehouse TeacherMina Basem ah gratz on figuring it out!
Mina Basem
Courses Plus Student 1,071 PointsMina Basem
Courses Plus Student 1,071 PointsThank you for the help! :)