DEV Community

Lokesh
Lokesh

Posted on

Lists in Python

Introduction

A list is used to store multiple values in a single variable. Instead of creating separate variables for each value, we can store all the values together in a list.

What Is a List in Python?

A list is a collection of elements stored in a single variable.

Lists are:

  • Ordered: Elements maintain their insertion order.
  • Mutable: We can add, remove, or modify elements.
  • Allow duplicates: The same value can appear more than once.
  • Allow different data types: A list can contain numbers, strings, and other data types.

How to Create a List in Python?

In Python, lists are created using square brackets []. Elements are separated by commas.

Example:

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

print(fruits)
Enter fullscreen mode Exit fullscreen mode

Output:

['Apple', 'Banana', 'Mango']
Enter fullscreen mode Exit fullscreen mode

We can also create a list with different data types.

my_list = [10, "Python", 25.5, True]

print(my_list)
Enter fullscreen mode Exit fullscreen mode

Output:

[10, 'Python', 25.5, True]
Enter fullscreen mode Exit fullscreen mode

List Indexing in Python

Indexing is used to access individual elements from a list.

In Python, indexing starts from 0.

There are two types of indexing:

  • Positive Indexing: Starts from the beginning (0, 1, 2, 3...).
  • Negative Indexing: Starts from the end (-1, -2, -3...).

Example:

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

print(fruits[0])
print(fruits[1])
print(fruits[-1])
print(fruits[-2])
Enter fullscreen mode Exit fullscreen mode

Output:

Apple
Banana
Orange
Mango
Enter fullscreen mode Exit fullscreen mode

List Slicing in Python

List slicing is used to access a specific portion of a list.

Syntax:

list[start:end:step]
Enter fullscreen mode Exit fullscreen mode
  • start: Starting index.
  • end: Ending index (not included).
  • step: Number of positions to move.

Example:

numbers = [10, 20, 30, 40, 50, 60]

print(numbers[0:3])
print(numbers[2:5])
print(numbers[:4])
print(numbers[::2])
Enter fullscreen mode Exit fullscreen mode

Output:

[10, 20, 30]
[30, 40, 50]
[10, 20, 30, 40]
[10, 30, 50]
Enter fullscreen mode Exit fullscreen mode

List Operations in Python

Python supports different operations on lists.

1. Concatenation

Concatenation is used to combine two or more lists using the + operator.

Example:

list1 = [10, 20, 30]
list2 = [40, 50, 60]

result = list1 + list2

print(result)
Enter fullscreen mode Exit fullscreen mode

Output:

[10, 20, 30, 40, 50, 60]
Enter fullscreen mode Exit fullscreen mode

2. Repetition

The * operator is used to repeat the elements of a list.

Example:

numbers = [1, 2, 3]

print(numbers * 2)
Enter fullscreen mode Exit fullscreen mode

Output:

[1, 2, 3, 1, 2, 3]
Enter fullscreen mode Exit fullscreen mode

3. Membership

Membership operators are used to check whether an element exists in a list.

The operators are:

  • in
  • not in

Example:

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

print("Apple" in fruits)
print("Orange" not in fruits)
Enter fullscreen mode Exit fullscreen mode

Output:

True
True
Enter fullscreen mode Exit fullscreen mode

Common List Methods in Python

Python provides several built-in methods to perform different operations on lists.

Method Description
append() Adds an element at the end of the list.
insert() Adds an element at a specified position.
extend() Adds multiple elements from another iterable.
remove() Removes the specified element.
pop() Removes and returns an element.
clear() Removes all elements from the list.
sort() Sorts the list.
reverse() Reverses the order of elements.
index() Returns the index of an element.
count() Counts how many times an element appears.
copy() Creates a shallow copy of the list.

Example of List Methods

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

fruits.append("Orange")
print(fruits)

fruits.insert(1, "Grapes")
print(fruits)

fruits.remove("Banana")
print(fruits)

fruits.pop()
print(fruits)
Enter fullscreen mode Exit fullscreen mode

Output:

['Apple', 'Banana', 'Mango', 'Orange']
['Apple', 'Grapes', 'Banana', 'Mango', 'Orange']
['Apple', 'Grapes', 'Mango', 'Orange']
['Apple', 'Grapes', 'Mango']
Enter fullscreen mode Exit fullscreen mode

Updating Elements in a List

Since lists are mutable, we can change their elements after creating them.

Example:

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

fruits[1] = "Orange"

print(fruits)
Enter fullscreen mode Exit fullscreen mode

Output:

['Apple', 'Orange', 'Mango']
Enter fullscreen mode Exit fullscreen mode

Here, the element at index 1 is changed from Banana to Orange.

Iterating Through a List Using a For Loop

We can use a for loop to access each element in a list one by one.

Example:

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

for fruit in fruits:
    print(fruit)
Enter fullscreen mode Exit fullscreen mode

Output:

Apple
Banana
Mango
Enter fullscreen mode Exit fullscreen mode

Nested Lists in Python

A nested list is a list that contains another list as an element.

Example:

numbers = [[10, 20], [30, 40], [50, 60]]

print(numbers)
print(numbers[0])
print(numbers[1][0])
Enter fullscreen mode Exit fullscreen mode

Output:

[[10, 20], [30, 40], [50, 60]]
[10, 20]
30
Enter fullscreen mode Exit fullscreen mode

Here, numbers[1][0] accesses the first element of the second inner list.

Why Are Lists Important in Python?

Lists are important because:

  1. They allow us to store multiple values in one variable.
  2. They make it easy to add, remove, and update elements.
  3. They help organize and manage data.
  4. They are useful for storing collections of information.
  5. They make data processing easier.

Top comments (0)