r/learnpython • • Aug 05 '22

BEGINNER CODER

Hey Peeps! I'm new to python and working and working on code academy. I'm tackling a project called “Len’s Slice” where I have to .insert() [2.5, “peppers”] into an index for a sublist. Please help! I'm stumped!

0 Upvotes

7 comments sorted by

View all comments

8

u/ryan770 Aug 05 '22

the syntax is list.insert(index, element)

so for:

list1 = [1, 2, 3, 4, 5]

if we do this:

list1.insert(3, [2.5, 'peppers'])

what we're doing is inserting a nested list at index 3 (the 4th position, indexes start at 0), and moving the rest of the elements over one.

print(list1)

outputs:

[1, 2, 3, [2.5, 'peppers'], 4, 5]

1

u/bahhhtam88 Aug 05 '22

Thanks! I looked back at the problem and I had “pizza_and_prices.insert(3, 2.5, “peppers”). I knew this was super easy for you but do you have any tips on how I can get better at python? Especially, retaining functions

3

u/ryan770 Aug 05 '22 edited Aug 05 '22

Try to get down the syntax difference between methods and functions, and what each does. The hard part is remembering if the thing you're doing is a method or a function! Because by themselves, they look the same.

For example:

insert() and sorted() look like you'd use them the same right?

but insert() is a method (specifically for lists) that directly affects the object attached to it. and sorted() is a function that takes an argument and returns a new object. It might be assigned to a variable or used as an argument itself.

For the variable my_variable, the method syntax is my_variable.method(), and function syntax is function(my_variable). Although a method can still have parameters inside the parentheses, as we've seen with insert()

So let's look at the method insert() again, and then use the function sorted().

my_list = [9,1,8,2,7]   # creating a new list

my_list.insert(1,3)   # inserting integer 3 at index 1.

print(my_list)   # prints: [9, 3, 1, 8, 2, 7], verifying the change.

sorted(my_list)  # calling the sorted() function, nothing happens.
                 # If you're in an interpreter shell, you may see the
                 # returned output though.

print(my_list)   # nothing changed. The sorted() function returned
                 # a new list but nothing was done with it.

list2 = sorted(my_list)   # now we've assigned the return of the
                          # sorted() function to a new variable
                          # called list2. 

print(list2)   # returns [1, 2, 3, 7, 8, 9], our new sorted list.

Now there is also a sort() method! my_list.sort() directly changes the list my_list, while sorted(my_list) returns a new sorted list.

There's a lot more to know about methods vs function (we'd have to get into class/object association, and even I'm just barely learning this).

but the jist is that methods are called on an object and can alter that objects state, while a function just operates on the object (without changing it) and returns a new value.

I'm not a teacher, and honestly I'm still a Python noob, I'm literally still in a Python class I'm about to take an exam for, but I hope this helps at least a little bit.

2

u/bahhhtam88 Aug 05 '22

This help a lot! Thank you and good luck!

1

u/bahhhtam88 Aug 05 '22

Wait! since we’re both noobs, we should prob work together(although I'm not sure how)