This python tutorial help to understand the list and useful methods. The list is the most popular and versatile datatype used in Python. It’s the same as an array as like other programming languages.
The main difference between array and list is, The list can contain different types of data items whereas the array can contain the same type of element.
You can also checkout other python list tutorials:
- Check Element Exist in List
- How to Filter a List in Python
- Python Join List Example
- How To Compare Python Two Lists
- How to Concatenate Two List in Python
- How To Match String Item into List Python
I’ll discuss, how to create a list, slicing of a list and adding or removing elements from the list, clear the list, and copy list items etc.
How To Create A List
You can define an empty list using square brackets:
list_items = ["Red", "Green", "Yellow"] print(list_items)
How To Access List Items
You can access the list items by referring to the index number:
list_items = ["Red", "Green", "Yellow"] print(list_items[1])
The Above code will print the second item of the list.
Negative Indexing Into Python List
Negative indexing means beginning from the end, -1
refers to the last item, -2
refers to the second-last item, etc.
list_items = ["Red", "Green", "Yellow"] print(list_items[-1])
The Above code will print the last item of the list.
Slicing of Python List
We can specify a range of indexes by specifying where to start and where to end the range. The return value will be a new list with the specified items.
list_items = ["Red", "Green", "Yellow", "Orange", "Pink"] print(list_items[1:3])
Output:
["Green", "Yellow"]
Change Item Value
To change the value of a specific item, refer to the index number:
list_items = ["Red", "Green", "Yellow"] list_items[1] = "Test" print(list_items)
Output:
["Green", "Test", "Orange"]
Loop Through a List
You can loop through the list items by using a for loop:
list_items = ["Red", "Green", "Yellow"] for x in list_items: print(x)
The above code will print all items from the list.
Check if Item Exists
To determine if a specified item is present in a list use the in a keyword:
list_items = ["Red", "Green", "Yellow"] if "Green" in list_items: print("Yes, 'Green' is in the color list")
The above code will check if “Green” is present in the list.
How To Count Length
Python has inbuilt len() method, that is used to determine the length of the item list.
list_items = ["Red", "Green", "Yellow"] print(len(list_items)) #2
clear() the List Element
This removes all the elements from the list and it will present you with a list clear of all elements.
import array as arr a = arr.array('i', [1, 3, 4, 8]) a.clear()
copy() the List Elements
The copy()
method returns a copy of the list elements.
import array as arr a = arr.array('i', [1, 3, 4, 8]) b = a.copy() print(a) print(b)
Add Items
The append()
method is used to add items to the end of the list.
list_items = ["Red", "Green", "Yellow"] list_items.append("Orange") print(list_items)
Output:
["Red", "Green", "Yellow", "Orange"]
How To Insert Item into Specific index
The list has insert()
method to add an item at the specific index into the list.
list_items = ["Red", "Green", "Yellow"] list_items.insert(1, "Orange") print(list_items)
We are Inserting an item as the second position into the list.
count() the array Elements
This method returns number of elements in the array with a specified value.
a = arr.array('i', [1, 3, 4, 8]) a.count()
How To remove An Item From the Python List
The python list has numerous methods to delete an item from the list. You can use remove()
method to delete an item from list. The pop() method removes the specified index, It will delete the last item if the index is not specified.The del
keyword also use to remove an element from the list.
list_items = ["Red", "Green", "Yellow", "Orange"] list_items.remove("Green") #["Red", "Yellow", "Orange"] del(list_items[2]) #["Red", "Yellow"] list_items.pop() #["Red"]
How To Join Two Lists
There is a number of ways to join or concatenate, two or more lists in Python. One of the easiest ways is by using the + operator.
list_items1 = ["Red", "Green", "Yellow"] list_items2 = ["Danger", "Success", "Warning"] combined_list = list_items1 + list_items2 list_items1.extend(list_items2) print(list_items1) print(combined_list)
Output:
['Red', 'Green', 'Yellow', 'Danger', 'Success', 'Warning'] ['Red', 'Green', 'Yellow', 'Danger', 'Success', 'Warning']