Lists are a widely used data type in Python. In most programming languages, they are known as arrays. They follow a specific order and can be modified. In Python, list values are separated by commas and enclosed in square brackets.
# This program is an example of list in python
testList=["C","C++","C#","Java","Python"] # Defining list
print(testList) # Printing list
Result:
['C', 'C++', 'C#', 'Java', 'Python']
Accessing a Specific Item
To access a specific item, use its index number.
# This program is an example of list in python
testList=["C","C++","C#","Java","Python"] # Defining list
print(testList[0]) # Accessing a specific element in a list
Result:
C
You can also use negative indexing to access a specific element.
# This program is an example of list in python
testList=["C","C++","C#","Java","Python"] # Defining list
print(testList[-1]) # Accessing a specific element using negative indexing
Result:
Python
Slicing a List
To slice a list, use the colon operator, similar to strings.
# This program is an example of a list in python
testList=["C","C++","C#","Java","Python"] # Defining list
print(testList[1:3]) # slicing elements from the list
Result:
['C++', 'C#']
Adding Two Lists
To add two lists, use the plus operator (+).
# This program is an example of adding 2 list in python
testList=["C","C++","C#","Java","Python"] # Defining list
testList2=["Javascript","PHP"] # Defining list
print(testList+testList2) # adding 2 list
Result:
['C', 'C++', 'C#', 'Java', 'Python', 'Javascript', 'PHP']
Loop in a List
Looping through each element of a list using its index is straightforward.
# This program is an example of a loop list in python
testList=["C","C++","C#","Java","Python"] # Defining list
for i in testList:
print(i)
Result:
C
C++
C#
Java
Python
List Methods
Python provides several built-in methods for list manipulation. Below are some commonly used ones (in alphabetical order).
append()
Adds an element at the end of the list.
# This program is an example of append() method in python
testList=["C","C++","C#","Java","Python"] # Defining list
testList.append("PHP") # Appending an element
print(testList)
Result:
['C', 'C++', 'C#', 'Java', 'Python', 'PHP']
clear()
Removes all elements from the list.
# This program is an example of clear() method in python
testList=["C","C++","C#","Java","Python"] # Defining list
testList.clear() # Clearning a list
print(testList)
Result:
[]
copy()
Copies the list into another list.
# This program is an example of copy() method in python
testList=["C","C++","C#","Java","Python"] # Defining list
outputList=testList.copy() # copying a list
print(outputList)
Result:
['C', 'C++', 'C#', 'Java', 'Python']
count()
Counts the occurrences of a specific element in the list.
# This program is an example of count() method in python
testList=["C","C++","C#","Java","Python","C"] # Defining list
countSpecific=testList.count("C") # counting an specific element in a list
print(countSpecific)
Result:
2
extend()
Extends a list by appending elements from another list.
# This program is an example of extend() method in python
testList=["C","C++","C#","Java","Python"] # Defining list
testList2=["JavaScript","PHP"] # Defining list
testList.extend(testList2) # extending a list
print(testList)
Result:
['C', 'C++', 'C#', 'Java', 'Python', 'JavaScript', 'PHP']
index()
Returns the index of the first occurrence of a specified value.
# This program is an example of index() method in python
testList=["C","C++","C#","Java","Python"] # Defining list
listIndex=testList.index("Python") # getting position a list
print(listIndex)
Result:
4
insert()
Inserts an element at a specified position.
# This program is an example of insert() method in python
testList=["C","C++","C#","Java","Python"] # Defining list
testList.insert(3,"Swift") # inserting a list
print(testList)
Result:
['C', 'C++', 'C#', 'Swift', 'Java', 'Python']
len()
Returns the number of elements in the list.
# This program is an example of len() method in python
testList=["C","C++","C#","Java","Python"] # Defining list
lengthList=len(testList) # using len() method
print(lengthList)
Result:
5
max()
Returns the largest element in the list.
# This program is an example of max() method in python
testList=[100,200,50,500,30] # Defining list
maxValue=max(testList) # using max() method
print(maxValue)
Result:
500
min()
Returns the smallest element in the list.
# This program is an example of min() method in python
#testList=["C","C++","C#","Java","Python"] # Defining list
testList=[100,200,50,500,30] # Defining list
maxValue=min(testList) # using min() method
print(maxValue)
Result:
30
list()
Converts a tuple into a list.
# This program is an example of list() method in python
testList=("C","C++","C#","Java","Python") # Defining a tuple
outputList=list(testList) # using min() method
print(outputList)
Result:
['C', 'C++', 'C#', 'Java', 'Python']
pop()
Removes the element at a specified position.
# This program is an example of pop() method in python
testList=["C","C++","C#","Java","Python"] # Defining list
testList.pop(2) # pop an element from a list
print(testList)
Result:
['C', 'C++', 'Java', 'Python']
remove()
Removes the first occurrence of a specified element. Unlike pop(), which removes an element based on its index, remove() removes an element based on its value.
# This program is an example of remove() method in python
testList=["C","C++","C#","Java","Python"] # Defining list
testList.remove("C#") # removing an element from a list
print(testList)
Result:
['C', 'C++', 'Java', 'Python']
reverse()
Reverses the order of elements in the list.
# This program is an example of reverse() method in python
testList=["C","C++","C#","Java","Python"] # Defining list
testList.reverse() # reversing a list
print(testList)
Result:
['Python', 'Java', 'C#', 'C++', 'C']
sort()
Sorts the list in ascending order by default.
# This program is an example of sort() method in python
testList=["C","C++","C#","Java","Python"] # Defining list
testList.sort() # sorting a list in ascending order
print(testList)
Result:
['C', 'C#', 'C++', 'Java', 'Python']
But it can be sorted in descending order.
# This program is an example of sort() method in python
testList=["C","C++","C#","Java","Python"] # Defining list
testList.sort(reverse=True) # sorting a list in Descending order
print(testList)
Result:
['Python', 'Java', 'C++', 'C#', 'C']