Python – List Methods

List Methods

Python has a set of built-in methods that you can use on lists. Continue reading Python – List Methods

Python – Join Lists

Join Two Lists

There are several ways to join, or concatenate, two or more lists in Python.

One of the easiest ways are by using the + operator.

Example

Join two list:

list1 = ["a", "b", "c"]
list2 = [1, 2, 3]

list3 = list1 + list2
print(list3)

Another way to join two lists is by appending all the items from list2 into list1, one by one: Continue reading Python – Join Lists

Python – Copy Lists

Copy a List

You cannot copy a list simply by typing list2 =
list1
, because: list2 will only be a reference to list1, and changes made in list1 will automatically also be made in list2.


Use the copy() method

You can use the built-in List method copy() to copy a list. Continue reading Python – Copy Lists

Python – Sort Lists

Sort List Alphanumerically

List objects have a
sort()
method that will sort the list alphanumerically, ascending, by default:

Example

Sort the list alphabetically:

thislist = ["orange", "mango", "kiwi", "pineapple", "banana"]
thislist.sort()
print(thislist)

Example

Sort the list numerically:

thislist = [100, 50, 65, 82, 23]
thislist.sort()
print(thislist)

Sort Descending

To sort descending, use the keyword argument reverse = True: Continue reading Python – Sort Lists

Python – List Comprehension

List Comprehension

List comprehension offers a shorter syntax when you want to create a new list based on the values of an existing list.

Example:

Based on a list of fruits, you want a new list, containing only the fruits with the letter “a” in the name.

Without list comprehension you will have to write a for statement with a conditional test inside: Continue reading Python – List Comprehension

Python – Loop Lists

Loop Through a List

You can loop through the list items by using a for loop:

Example

Print all items in the list, one by one:

thislist = ["apple", "banana", "cherry"]
for x in thislist:
  print(x)

Continue reading Python – Loop Lists

Python – Remove List Items

Remove Specified Item

The remove() method removes the specified item.

Example

Remove “banana”:

thislist = ["apple", "banana", "cherry"]
thislist.remove("banana")
print(thislist)

If there are more than one item with the specified value, the remove() method removes the first occurrence: Continue reading Python – Remove List Items

Python – Add List Items

Append Items

To add an item to the end of the list, use the append() method:

Example

Using the append() method to append an item:

thislist = ["apple", "banana", "cherry"]
thislist.append("orange")
print(thislist)

Insert Items

To insert a list item at a specified index, use the insert() method.

The insert() method inserts an item at the specified index: Continue reading Python – Add List Items

Python – Change List Items

Change Item Value

To change the value of a specific item, refer to the index number:

Example

Change the second item:

thislist = ["apple", "banana", "cherry"]
thislist[1] = "blackcurrant"
print(thislist)

Change a Range of Item Values

To change the value of items within a specific range, define a list with the new values, and refer to the range of index numbers where you want to insert the new values: Continue reading Python – Change List Items

Python – Access List Items

Access Items

List items are indexed and you can access them by referring to the index number:

Example

Print the second item of the list:

thislist = ["apple", "banana", "cherry"]
print(thislist[1])

Note: The first item has index 0.

Negative Indexing

Negative indexing means start from the end

-1 refers to the last item, -2 refers to the second last item etc. Continue reading Python – Access List Items