List/Array Methods
What You'll Learn: In this tutorial, you'll discover various built-in methods you can use to work with lists in Python. These methods allow you to manipulate, analyze, and organize list items efficiently.
List Methods and Descriptions
append()- Description: Adds an element at the end of the list.
Example Code:
pythonthislist = ["apple", "banana", "cherry"] thislist.append("orange") print(thislist)Output:
['apple', 'banana', 'cherry', 'orange']
clear()- Description: Removes all the elements from the list.
Example Code:
pythonthislist = ["apple", "banana", "cherry"] thislist.clear() print(thislist)Output:
[]
copy()- Description: Returns a copy of the list.
Example Code:
pythonthislist = ["apple", "banana", "cherry"] mylist = thislist.copy() print(mylist)Output:
['apple', 'banana', 'cherry']
count()- Description: Returns the number of elements with the specified value.
Example Code:
pythonthislist = ["apple", "banana", "cherry", "banana"] print(thislist.count("banana"))Output:
2
extend()- Description: Adds the elements of a list (or any iterable) to the end of the current list.
Example Code:
pythonthislist = ["apple", "banana", "cherry"] tropical = ["mango", "pineapple", "papaya"] thislist.extend(tropical) print(thislist)Output:
['apple', 'banana', 'cherry', 'mango', 'pineapple', 'papaya']
index()- Description: Returns the index of the first element with the specified value.
Example Code:
pythonthislist = ["apple", "banana", "cherry"] print(thislist.index("cherry"))Output:
2
insert()- Description: Adds an element at the specified position.
Example Code:
pythonthislist = ["apple", "banana", "cherry"] thislist.insert(1, "orange") print(thislist)Output:
['apple', 'orange', 'banana', 'cherry']
pop()- Description: Removes the element at the specified position.
Example Code:
pythonthislist = ["apple", "banana", "cherry"] thislist.pop(1) print(thislist)Output:
['apple', 'cherry']
remove()- Description: Removes the item with the specified value.
Example Code:
pythonthislist = ["apple", "banana", "cherry"] thislist.remove("banana") print(thislist)Output:
['apple', 'cherry']
reverse()- Description: Reverses the order of the list.
Example Code:
pythonthislist = ["apple", "banana", "cherry"] thislist.reverse() print(thislist)Output:
['cherry', 'banana', 'apple']
sort()- Description: Sorts the list.
Example Code:
pythonthislist = ["orange", "mango", "kiwi", "pineapple", "banana"] thislist.sort() print(thislist)Output:
['banana', 'kiwi', 'mango', 'orange', 'pineapple']
Try It Yourself: Fun Exercises
- Organize Your Playlist:
- Create a list of your favorite songs.
- Use
sort()to arrange them alphabetically.
- Manage Your Shopping List:
- Create a shopping list.
- Use
append(),remove(), andreverse()to manage your list.
Summary:
In this Python tutorial, we learned about various list methods including append(), clear(), copy(), count(), extend(), index(), insert(), pop(), remove(), reverse(), and sort(). These methods help you manage and manipulate list items efficiently. Keep experimenting and have fun with lists in Python!