Python List

Python list is ordered data type i.e. with heterogeneous data. List are enclosed in brackets [] with items comma separated. The list is the most widely used data type in Python.

How to create a list in Python?

List in Python is created using big brackets [] with the items in the list be separated by a comma (,).  A list can have any number of items and can be of different data types. To have an idea of python data types, please refer to this tutorial:- Python Datatypes.

Let’s explore some of the examples of the list in the following example:-

emptyList = []
colors = ['red', 'green', 'blue']
numbers = [1, 2, 3, 4, 5, 6]
mixed = ['John', 103, 10.2]
matrix = [[1, 2, 3], [4, 5, 6]]

print(emptyList)
print(colors)
print(numbers)
print(mixed)
print(matrix)

The output of the above program is:-

[]
[‘red’, ‘green’, ‘blue’]
[1, 2, 3, 4, 5, 6]
[‘John’, 103, 10.2]
[[1, 2, 3], [4, 5, 6]]

How to access elements in a list in Python?

We can use a slice operator [] and [:] to get items in the list. The concatenation operation can be done using plus (+) sign and asterisk(*) serves as the repetition operator. We can use two types of indexing to get items viz. positive indexing and negative indexing. Positive indexing allows to index items from 0 to n where negative index gives access from backward, where -1 is the last item.

Below is an example of list with simple operations:-

list = ['abcd', 786 , 2.23, 'john', 70.2]
print(list)          # Prints complete list
print(list[0])       # Prints first element of the list
print(list[1:3])     # Prints elements starting from 2nd till 3rd
print(list[2:])      # Prints elements starting from 3rd element
print(list[-1])      # Prints last element

Output:-

[‘abcd’, 786, 2.23, ‘john’, 70.2]
abcd
[786, 2.23]
[2.23, ‘john’, 70.2]
70.2

How to change or add elements to a list in Python?

Since lists are mutable data types, you can add, edit or delete every item in a list. It is the most flexible data type in Python. You can use a particular index or slice operator to select more than one items to update the value of the list in that position.

colors = ['red', 'green', 'blue']
print("Current list", colors)
colors[1] = 'black'
print("Updated new color", colors)
It gives the following output:-
Current list [‘red’, ‘green’, ‘blue’]
Updated new color [‘red’, ‘black’, ‘blue’]

To add elements in the list, append() method of the list is used.

colors = ['red', 'green', 'blue']
print("Current list", colors)
colors.append('gray')
print("Updated new color", colors)
Current list [‘red’, ‘green’, ‘blue’]
Updated new color [‘red’, ‘green’, ‘blue’, ‘gray’]

 How to delete or remove elements from a list in Python?

We can use del function to remove the whole list or index of items in the list.

colors = ['red', 'green', 'blue', 'gray', 'black']
print("Current list", colors)
del(colors[1])  #removes the color green
print("Updated list", colors)
del(colors[1:3])
print("Updated list", colors)
Current list [‘red’, ‘green’, ‘blue’, ‘gray’, ‘black’]
Updated list [‘red’, ‘blue’, ‘gray’, ‘black’]
Updated list [‘red’, ‘black’]

How to iterate over a List in Python?

A list can be iterated using for loop. Let us see the following example:-

colors = ['red', 'green', 'blue', 'gray', 'black']
for color in colors:
    print(color)
red
green
blue
gray
black

What are Python List Operations?

OperatorDescription
+Concatenation of two lists
*Repetition operator, to repeat list in multiple times
inMembership operator to check if an item exists in a list
not inMembership operator to check if an item does not exist in a list

What are Python List Built-in functions?

Python provides the following built-in functions which can be used with the lists.

FunctionDescription
cmp(list1, list2)Compares the elements of left and right lists
len(list)Calculate the length of the list
max(list)Returns the maximum element of the list
min(list)Returns the minimum element of the list
list(seq)Converts any sequence to the list
sum()Returns the sum of all elements in the list
sorted()
Returns the new sorted list
enumerate()Returns an enumerated object
all()Returns true if all elements in the list are True
any()Returns true if any element in list is True

What are Python List built-in methods?

Python provides various methods that can help you perform the various operations in a list. Following are some of the built-in list methods:-

MethodDescription
append()Add an element to the end of the list
count()Returns the count of the number of items passed as an argument
extend() Add all elements of a list to the another list
index() Returns the index of the first matched item
insert() Insert an item at the defined index
pop() Removes and returns an element at the given index
copy()Returns a shallow copy of the list
remove()Removes an item from the list
reverse()Reverse the order of items in the list
sort()Sort items in a list in ascending order
clear()Removes all items from the list

 

SHARE Python List

You may also like...

Leave a Reply

Your email address will not be published.

Share