Python List


In this tutorial, we will learn about python list

 

What is list in python ?


A list is a versatile data structure in Python that allows storing a collection of values in a single variable.

A list can contain elements of different data types, including integers, floats, strings, and even other lists.

Lists are mutable, meaning that you can modify their contents by adding, removing, or modifying elements.

In this tutorial, we will explore the basics of lists in Python, including how to create, manipulate, and access list elements.

 

Creating a List


You can create a list in Python by enclosing a comma-separated sequence of elements inside square brackets [].

For example, to create a list of integers, you can write:

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


Similarly, to create a list of strings, you can write:

fruits = ["apple", "banana", "cherry", "date"]


You can also create a list of mixed data types, such as:

mixed = [1, "apple", 3.14, True]


Accessing List Elements


You can access individual elements of a list by using their index, which starts from zero.

To access the first element of a list, you can write:

numbers = [1, 2, 3, 4, 5]
print(numbers[0])

#Output :
#1

 

In the above example, we create a list of numbers and print the first element of the list using its index, which is zero-based.

You can also use negative indexing to access elements from the end of the list.

For example, to access the last element of a list, you can write:

numbers = [1, 2, 3, 4, 5]
print(numbers[-1])

#Output :
#5

 

Slicing a List


You can extract a portion of a list by using slicing.

Slicing a list means accessing a range of elements from the list.

The syntax for slicing a list is [start:end:step], where start is the index of the first element to include, end is the index of the first element to exclude, and step is the increment between the elements.

For example, to extract the first three elements of a list, you can write:

numbers = [1, 2, 3, 4, 5]
print(numbers[0:3])

#Output :
#[1, 2, 3]


In the above example, we use slicing to extract the first three elements of the list. The start index is zero, which means the first element of the list, and the end index is three, which means the fourth element of the list is excluded.

You can also use negative indices in slicing.

For example, to extract the last three elements of a list, you can write:

numbers = [1, 2, 3, 4, 5]
print(numbers[0:3])

#Output :
#[3, 4, 5]

In the above example, we use negative indexing to specify the range of elements to extract. The start index is -3, which means the third-last element of the list, and the end index is omitted, which means the last element of the list.

 

Modifying List Elements


You can modify individual elements of a list by using their index.

For example, to change the second element of a list, you can write:

numbers = [1, 2, 3, 4, 5]
numbers[1] = 7

#Output :
#[1, 7, 3, 4, 5]

In the above example, we modify the second element of the numbers list from 2 to 7 by assigning a new value to its index.

 

Adding Elements to a List


You can add elements to a list by using the append() method, which adds an element to the end of the list.

For example, to add a new element to a list, you can write:

numbers = [1, 2, 3, 4, 5]
numbers.append(6)
print(numbers)

#Output :
#[1, 2, 3, 4, 5, 6]


In the above example, we use the append() method to add a new element 6 to the end of the numbers list.

You can also add multiple elements to a list by using the extend() method or the + operator.

For example, to add multiple elements to a list, you can write:

numbers = [1, 2, 3, 4, 5]
numbers.extend([6, 7, 8])
print(numbers)

#Output :
#[1, 2, 3, 4, 5, 6, 7, 8]


Or :

numbers = [1, 2, 3, 4, 5]
numbers = numbers + [6, 7, 8]
print(numbers)

#Output :
#[1, 2, 3, 4, 5, 6, 7, 8]


Removing Elements from a List


You can remove elements from a list by using the remove() method or the del keyword.

The remove() method removes the first occurrence of the specified element, while the del keyword removes the element at the specified index.

For example, to remove an element from a list, you can write:

numbers = [1, 2, 3, 4, 5]
numbers.remove(3)
print(numbers)

#Output :
#[1, 2, 4, 5]


In the above example, we use the remove() method to remove the element 3 from the numbers list.

You can also use the del keyword to remove an element by its index.

For example, to remove the second element of a list, you can write:

numbers = [1, 2, 3, 4, 5]
del numbers[1]
print(numbers)

#Output :
#[1, 3, 4, 5]


In the above example, we use the del keyword to remove the second element of the numbers list, which is 2.

 

List Methods


Python provides many built-in methods for manipulating lists. Here are some common list methods:

append() - adds an element to the end of the list.
extend() - adds multiple elements to the end of the list.
insert() - inserts an element at the specified index.
remove() - removes the first occurrence of the specified element.
pop() - removes and returns the element at the specified index.
clear() - removes all elements from the list.
index() - returns the index of the first occurrence of the specified element.
count() - returns the number of occurrences of the specified element.
sort() - sorts the elements of the list in ascending order.
reverse() - reverses the order of the elements in the list.
copy() - returns a copy of the list.

Let's look at some examples of how to use these list methods:
 

fruits = ['apple', 'banana', 'cherry']
fruits.append('orange')
print(fruits) # ['apple', 'banana', 'cherry', 'orange']

fruits.extend(['grape', 'kiwi'])
print(fruits) # ['apple', 'banana', 'cherry', 'orange', 'grape', 'kiwi']

fruits.insert(2, 'pear')
print(fruits) # ['apple', 'banana', 'pear', 'cherry', 'orange', 'grape', 'kiwi']

fruits.remove('orange')
print(fruits) # ['apple', 'banana', 'pear', 'cherry', 'grape', 'kiwi']

fruit = fruits.pop(2)
print(fruit)  # 'pear'
print(fruits) # ['apple', 'banana', 'cherry', 'grape', 'kiwi']

fruits.clear()
print(fruits) # []

fruits = ['apple', 'banana', 'cherry', 'banana']
print(fruits.index('banana')) # 1
print(fruits.count('banana')) # 2

fruits.sort()
print(fruits) # ['apple', 'banana', 'banana', 'cherry']

fruits.reverse()
print(fruits) # ['cherry', 'banana', 'banana', 'apple']

new_fruits = fruits.copy()
print(new_fruits) # ['cherry', 'banana', 'banana', 'apple']

 

 

 

© 2022-2023 All rights reserved.