What is a List in Python?

The Python List is one of the most useful sequences in real-time. A Python list is a sequence of multiple values in an ordered sequence. Unlike Strings, it allows us to store different types of data such as integer, float, string, etc.

How to declare Python Lists

There are several ways to create a List in Python. The most straightforward way is to place the required items within a square bracket [ ].

How do you create an Empty List?

An object that contains no values or elements is empty and placing a square bracket creates an empty list in python language.

ListName = []

How to create a Python List of Different Data Types?

The first statement is an integer of five integer values or multiple elements within the square brackets. The second statement is a string that contains three String values or three words.

IntegerList = [1, 2, 3, 4, 5]

StringList = [‘apple’, ‘Orange’, ‘Grape’, ‘Mango’]

Python lists allow placing different data types in a single. It is an example of a mixed, which contains one integer, float, and the two integer values.

MixedList = [‘apple’, 2, 3.50, ‘Mango’]

How to access Python List items?

Python Lists sequentially store data (ordered). So, we can access the elements with the help of indexes. Moreover, using indexes, we can access or alter/change each item present in it separately. The syntax to access items is

ListName([IndexNumber])

Index value starts at 0 and ends at n-1, where n is the size. For example, if it stores 5 elements, the index starts at 0 and ends with 4. To access or alter the first list value, use list_name[0], and to access the fifth list item, use list_name[4]. Let’s see the Python list example for better understanding:

x = [1, 2, 3, 4, 5, 6, 7, 8, 9]

# Positive Indexing
print(x[0])
print(x[4])
print(x[8])
print('=======\n')

# Negative
print(x[-2])
print(x[-4])
print(x[-8])

Elements output of Positive and Negative Indexes

1
5
9
=======

8
6
2

Please use Negative numbers as the index to access the elements from right to left. It means access Python list items in reverse order.

x = ['apple', 'Mango', 'banana', 'orange', 'cherry','kiwi']

# Using Positive 
print("IP 0 = ", x[0])
print("IP 2 = ", x[2])
print("IP 4 = ", x[4])
print("IP 3 = ", x[3])
print('=======\n')

#Using Negative 
print("Pos -1 = ", x[-1])
print("Pos -3 = ", x[-3])
print("Pos -5 = ", x[-5])
print("Pos -6 = ", x[-6])

Accessing elements elements using both the Positive and Negative Numbers

IP 0 =  apple
IP 2 =  banana
IP 4 =  cherry
IP 3 =  orange
=======

Pos -1 =  kiwi
Pos -3 =  orange
Pos -5 =  Mango
Pos -6 =  apple

Alter Python List Items

Since there are mutable, apart from accessing items, use this index positions to alter or replace the elements.

Python List alter

Iterate Python List items

A For Loop is the most common way to traverse the list items and it helps to iterate and print the items. This code works accurately to print items inside it. However, to alter the individual element, we need the index position.

To resolve this, we have to use the range function along with Python for loop.

Fruits = ['Apple', 'Orange', 'Grape', 'Banana']
for Fruit in Fruits:
    print(Fruit)

It multiplies each item with 10. If we want to perform the calculation based on condition, then use If Statement inside the for loop.

x = [1, 2, 3, 4, 5, 6, 7, 8, 9]

for Number in range(len(x)):
    x[Number] = x[Number] * 10
print(x)
Python List Iteration

In this example, we declared a String. The first for loop is to iterate and print the items. And the second for loop, along with the range, to iterate each element using the position of an index. Let us see another example.

# Iterate Items

Fruits = ['apple', 'Mango', 'banana', 'orange', 'cherry','kiwi']

# Iterate Elements
for fruit in Fruits:
    print(fruit)

# Iterate Items using Index 
for i in range(len(Fruits)):
    print("Item at ", i, " = ", Fruits[i])

Iterating the String list items using for loop and for loop range output

apple
Mango
banana
orange
cherry
kiwi
Item at 0  =  apple
Item at 1  =  Mango
Item at 2  =  banana
Item at 3  =  orange
Item at 4  =  cherry
Item at 5  =  kiwi

Insert items into Python List

The available built in functions to insert new items into the existing one.

  1. Append(x): The append method adds item x at the end.
  2. Insert(i, x): The insert method inserts the specified item x at position i.
  3. Extend(New_List): The extend method adds all the elements in New_List at the end.
Fruits = ['Apple', 'Orange', 'Grape', 'Banana']

# Adding items using append
Fruits.append('Blackberry')
print(Fruits)

# inserting items using insert
Fruits.insert(2, 'Kiwi')
print(Fruits)

# Extending using extend
Fruit_new = ['berry','Cherry']
Fruits.extend(Fruit_new) 
print(Fruits)
['Apple', 'Orange', 'Grape', 'Banana', 'Blackberry']
['Apple', 'Orange', 'Kiwi', 'Grape', 'Banana', 'Blackberry']
['Apple', 'Orange', 'Kiwi', 'Grape', 'Banana', 'Blackberry', 'berry', 'Cherry']

Python List Slicing

In python List Slice, the first integer value is the index position where the slicing start and the second integer value is the index position where the slicing end. The Slicing go untill the second integer value but not include the value at this end index position. For instance, if we specify a [1:4], then slicing start at index position one and end at 3 (not 4)

x = [1, 2, 3, 4, 5, 6, 7, 8, 9]

# Slicing using two indexes
a = x[2:6] 
print(a)

# Slicing using First
b = x[:6] 
print(b)

# Slicing using Second
c = x[2:] 
print(c)

# Slicing without using two
d = x[:] 
print(d)
[3, 4, 5, 6]
[1, 2, 3, 4, 5, 6]
[3, 4, 5, 6, 7, 8, 9]
[1, 2, 3, 4, 5, 6, 7, 8, 9]

Use Negative numbers as the values to slice the elements.

x = [1, 2, 3, 4, 5, 6, 7, 8, 9]

# Slicing using Negative first
e = x[-3:] 
print(e)

# Slicing using Negative Second
f = x[:-2] 
print(f)

# Slicing using Negative first and second
g = x[-7:-2] 
print(g)

# Assigning new values
x[1:3] = ['t','g']
print(x)
[7, 8, 9]
[1, 2, 3, 4, 5, 6, 7]
[3, 4, 5, 6, 7]
[1, 't', 'g', 4, 5, 6, 7, 8, 9]

From the above slicing

  • Omit the first index means the Slicing start from the beginning.
  • Omit the second, slicing start from the first index and continue to the last.
  • Use the Negative values to Slice the elements from right to left.

Built in Functions

In this Python Lists program, we are applying all the built in methods.

Fruits = ['Apple', 'Orange', 'Banana', 'Kiwi', 'Grape', 'Blackberry']
x = [9, 4, -5, 0, 22, -1, 2, 14]

#Copying using Copy() Method
New_Fruits = Fruits.copy()
print(New_Fruits)

#Removing all the items using Clear() Method
New_Fruits.clear()
print(New_Fruits)

# Sorting using Sort() Method
Fruits.sort()
x.sort()
print(Fruits)
print(x)

# Reverse using reverse() Method
Fruits.reverse()
x.reverse()
print(Fruits)
print(x)

# position of an item
print('The Index position of Banana = ', Fruits.index('Banana'))
print('The Index position of -1 = ', x.index(-1))

# Counting items using count() Method
y = [9, 4, 1, 4, 9, -1, 2, 4]
print('Number of Times 4 is repeated = ', y.count(4))
print('Number of Times 9 is repeated = ', y.count(9))
['Apple', 'Orange', 'Banana', 'Kiwi', 'Grape', 'Blackberry']
[]
['Apple', 'Banana', 'Blackberry', 'Grape', 'Kiwi', 'Orange']
[-5, -1, 0, 2, 4, 9, 14, 22]
['Orange', 'Kiwi', 'Grape', 'Blackberry', 'Banana', 'Apple']
[22, 14, 9, 4, 2, 0, -1, -5]
The Index position of Banana =  4
The Index position of -1 =  6
Number of Times 4 is repeated =  3
Number of Times 9 is repeated =  2

sum

The List sum function finds the sum of all items.

a = [5, 10, 15, 20, 25]
print(a)

# sum of elements
total = sum(a)

print("\nThe sum = ", total)
[5, 10, 15, 20, 25]

The sum =  75

Python List Arithmetic Operations Example

Using the Arithmetic Operators to perform arithmetic operations.

  • + operator is concatenating them.
  • * operator is repeating the element for a given number of times. Here it is three times.
x = [10, 20, 30, 40]
y = [15, 25, 35, 45]

# using + Operator
total = x + y
print("\nAddition : ", total)

# using * Operator
multi = x * 2
print("Multiplication : ", multi)

multi2 = y * 3
print("Multiplication of Y : ", multi2)

Performing arithmetic operations and return output.

Addition :  [10, 20, 30, 40, 15, 25, 35, 45]
Multiplication :  [10, 20, 30, 40, 10, 20, 30, 40]
Multiplication of Y :  [15, 25, 35, 45, 15, 25, 35, 45, 15, 25, 35, 45]