List Methods
Python has a set of built-in methods that you can use on lists. Continue reading Python – List Methods
Python has a set of built-in methods that you can use on lists. Continue reading Python – List Methods
There are several ways to join, or concatenate, two or more lists in Python.
One of the easiest ways are by using the +
operator.
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
You cannot copy a list simply by typing list2 =
, because:
list1list2
will only be a reference to list1
, and changes made in list1
will automatically also be made in list2
.
You can use the built-in List method copy()
to copy a list. Continue reading Python – Copy Lists
List objects have a
method that will sort the list alphanumerically, ascending, by default:
sort()
Sort the list alphabetically:
thislist = ["orange", "mango", "kiwi", "pineapple", "banana"]
thislist.sort()
print(thislist)
Sort the list numerically:
thislist = [100, 50, 65, 82, 23]
thislist.sort()
print(thislist)
To sort descending, use the keyword argument reverse = True
: Continue reading Python – Sort Lists
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
You can loop through the list items by using a for
loop:
Print all items in the list, one by one:
thislist = ["apple", "banana", "cherry"]
for x in thislist:
print(x)
The remove()
method removes the specified item.
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
To add an item to the end of the list, use the append() method:
Using the append()
method to append an item:
thislist = ["apple", "banana", "cherry"]
thislist.append("orange")
print(thislist)
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
To change the value of a specific item, refer to the index number:
Change the second item:
thislist = ["apple", "banana", "cherry"]
thislist[1] = "blackcurrant"
print(thislist)
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
List items are indexed and you can access them by referring to the index number:
Print the second item of the list:
thislist = ["apple", "banana", "cherry"]
print(thislist[1])
Note: The first item has index 0.
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