Python Crash Course Lists Methods! #3

 Hi Everyone,

Dumping more notes today for my own reference! Need more practice with these.

Modifying Elements in a List - 
The syntax for modifying an element is similar to the syntax for accessing
an element in a list. To change an element, use the name of the list followed
by the index of the element you want to change, and then provide the new
value you want that item to have.
motorcycles[0] = 'ducati'

Appending Elements to the End of a List - 
The simplest way to add a new element to a list is to append the item to the
list. When you append an item to a list, the new element is added to the end
of the list. Using the same list we had in the previous example, we’ll add the
new element 'ducati' to the end of the list:
motorcycles.append('ducati')

Inserting Elements into a List - 
You can add a new element at any position in your list by using the insert()
method. You do this by specifying the index of the new element and the
value of the new item.
motorcycles.insert(0, 'ducati')

Removing an Item Using the del Statement - 
If you know the position of the item you want to remove from a list, you can
use the del statement.
del motorcycles[0]

The pop() method removes the last item in a list, but it lets you work
with that item after removing it. The term pop comes from thinking of a
list as a stack of items and popping one item off the top of the stack. In
this analogy, the top of a stack corresponds to the end of a list.
popped_motorcycle = motorcycles.pop()

You can use pop() to remove an item from any position in a list by including
the index of the item you want to remove in parentheses.
first_owned = motorcycles.pop(0)

Remove item from list (different from del) - 
Sometimes you won’t know the position of the value you want to remove
from a list. If you only know the value of the item you want to remove, you
can use the remove() method.
motorcycles.remove('ducati')

Sorting a List Permanently with the sort() Method
Python’s sort() method makes it relatively easy to sort a list. Imagine we
have a list of cars and want to change the order of the list to store them
alphabetically. 
cars.sort()

You can also sort this list in reverse alphabetical order by passing the
argument reverse=True to the sort() method 
The following example sorts the list of cars in reverse alphabetical order:
cars.sort(reverse=True)

Sorting a List Temporarily with the sorted() Function
To maintain the original order of a list but present it in a sorted order, you
can use the sorted() function. The sorted() function lets you display your list
in a particular order but doesn’t affect the actual order of the list.
Let’s try this function on the list of cars.
print(sorted(cars))

Printing a List in Reverse Order
To reverse the original order of a list, you can use the reverse() method.
If we originally stored the list of cars in chronological order according to
when we owned them, we could easily rearrange the list into reverse chronological
order:
cars.reverse()
reverse() doesn’t sort backward alphabetically; it simply
reverses the order of the list.

Finding the Length of a List
You can quickly find the length of a list by using the len() function. The list
in this example has four items, so its length is 4:
len(cars)


Comments

Popular posts from this blog

Python Crash Course Chapter 1-2! #1

I PASSED! Cisco CCNA 200-301! Experience & Where I'm going from here!

Python Notes #6 Functions