Know all about Python List

Know all about Python list

What is a Python List?

A Python list is a collection of multiple values that can be assigned to a single variable.

How to create a Python List?

A Python list is created using square brackets [...], where inside the brackets we put values and each value is separated by a comma.

# A Python List
names = ["John", "Mark", "Baburao", "Raju"]

Multiple values can be of different data types

Inside a Python list, you can put different types of data, such as – string, number, boolean, etc.

myList = ["Rahul", 55, True]

Add list inside a list

A Python list is also a data type so you can put lists inside a List.

myList = ["Rahul", ["Hello", 21, 77, False], True, 11]

# OR
aList = ["Hello", 21, 77, False];
myList2 = ["Rahul", aList, True, 11]

Access individual values (items) of a list

With the help of the Index numbers (starting from 0) we can access individual values of a Python list.

index numbers of values of a Python List
fruits = ["Apple", "Banana", "Mango", "Strawberry"]

print(fruits[2])
print(fruits[0])
print(fruits[3])
print("My favorite fruit is " + fruits[1])
Mango
Apple
Strawberry
My favorite fruit is Banana

When list have lists

#            0        1         2          3
fruits = ["Apple", "Banana", "Mango", "Strawberry"]

#         0        1        2       3        4
food = ["Rice", "Paneer", fruits, "Roti", "Salad"]

print(food[2][3])
Strawberry

Python List Methods

Python has a lot of list methods that allow us to work with lists. Here are the methods with examples –

1. Insert new value to a Python List

The List.append() method is used to insert new values into an existing Python list. This method will add value at the end of the list, and the value can be of any type.

fruits = ["Apple", "Banana", "Mango", "Strawberry"]

fruits.append("Pineapple")

print(fruits)
['Apple', 'Banana', 'Mango', 'Strawberry', 'Pineapple']

2. Merge two Python lists

The List.extend() method is used to merge two Python lists. Or you can say this method will insert multiple values in a Python list at once.

fruits = ["Apple", "Banana", "Mango"]

fruits.extend(["Pineapple", "Strawberry"])

# Alternative Syntax
# fruits2 = ["Pineapple", "Strawberry"]
# fruits.extend(fruits2)

print(fruits)
['Apple', 'Banana', 'Mango', 'Pineapple', 'Strawberry']

You can also merge Tuples and Sets to a Python list

aList = ["Hello"]

aTuple = ("Java", "Python", "C++")

aSet = {221, 52, 221}

aList.extend(aTuple)
aList.extend(aSet)

print(aList)
['Hello', 'Java', 'Python', 'C++', 52, 221]

Difference between append and extend method of Python list

The append method only adds one item to the end of the list at a time. But the extend method takes an iterable as the parameter, so it inserts multiple elements at the end of the list at a time.


3. Length of a list

The len() method is used to get the length (number of values) of a Python list. This method is not part of the list methods.

fruits = ["Apple", "Banana", "Mango", "Strawberry"]

print(len(fruits))
4

4. Count an item of a list

The List.count() method is used to count the number of times a particular value is specified in a Python list.

fruits = ["Apple", "Banana", "Apple", "Mango", "Apple"]

print(fruits.count("Apple"))
3

5. Get the index number of a list item

The List.index() method is used to get the index number of a specific value (list item) of a Python list.

fruits = ["Apple", "Banana", "Mango", "Strawberry"]

print(fruits.index("Mango"))
2

6. Delete or Remove values from a list

The List.remove() method is used to remove the first match of the specified value from a Python list.

animals = ["Dog", "Cat", "Tiger", "Lion", "Horse", "Cat"]

animals.remove("Cat")

print(animals)
['Dog', 'Tiger', 'Lion', 'Horse', 'Cat']

Delete a value with index number

The List.pop() method is used to remove the item at a given index from a Python list.

animals = ["Dog", "Cat", "Tiger", "Lion", "Horse", "Cat"]

animals.pop(3)

print(animals)
['Dog', 'Cat', 'Tiger', 'Horse', 'Cat']

Delete all items from a list

If you want to remove all items from a list, you can use the List.clear() method –

animals = ["Dog", "Cat", "Tiger", "Lion", "Horse", "Cat"]

animals.clear()

print(animals)

# Alternative ways to delete all items from a list
# Way 1: animals = [] Assign a new empty list to the variable
# Way 2: del a[:] Use del statement with slice (:) operator
[]

7. Insert a value into a specified position in a list

The List.insert() method is used to insert an element at a specific position in a Python list.

This method takes two parameters – index number (where you want to insert an item) and value.

fruits = ["apple", "Mango", "Strawberry"]

fruits.insert(1, "Banana")

print(fruits)
['apple', 'Banana', 'Mango', 'Strawberry']

8. Reverse a Python List

The List.reverse() method is used to reverse a Python list.

fruits = ["Apple", "Mango", "Banana"]

fruits.reverse()

print(fruits)
['Banana', 'Mango', 'Apple']