0% found this document useful (0 votes)
5 views

Lists in Python

Uploaded by

sharath.s
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Lists in Python

Uploaded by

sharath.s
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

Yogesh Tyagi

@ytyagi782

Lists in
Python
ehe nsive
A Compr
Guide
Yogesh Tyagi
@ytyagi782

Lists in Python: A
Comprehensive Guide

Learn how to use Python lists


effectively for storing, managing,
and manipulating collections of data
in your programs.
Yogesh Tyagi
@ytyagi782

What is a List in Python?

A list is a collection data type in Python that


can hold multiple items in a single variable.

Lists are ordered, meaning items maintain


their insertion order.
Lists are mutable, meaning you can change
their content after creation.
They can hold items of different data types
(e.g., integers, strings, even other lists).

Examples:
Yogesh Tyagi
@ytyagi782

Creating Lists

Lists can be created in several ways:

Using square brackets ([]):

Using the list() constructor:

Creating an empty list:

Items in a list are separated by


Key
commas.
Features:
Lists can hold duplicate items.
Yogesh Tyagi
@ytyagi782

Accessing List Elements

List elements can be accessed using their index.

Python uses zero-based indexing: The first


element is at index 0.
Negative indexing allows access from the
end of the list.

Examples:

Accessing an index out of range


Note
will raise an IndexError.
Yogesh Tyagi
@ytyagi782

Accessing List Elements

Lists are mutable, so you can


modify their elements.

Changing a single element

Changing multiple elements:


Yogesh Tyagi
@ytyagi782

Adding Elements to a List

append() Adds a single element to the end of the list.

extend() Adds multiple elements to the list.

insert() Adds an element at a specific position.


Yogesh Tyagi
@ytyagi782

Removing Elements
from a List

Removes the first occurrence of a specific value.

remove()

Removes an element by index and returns it

pop()

Deletes an element or the entire list.


del
Statement

Empties the list.

clear()
Yogesh Tyagi
@ytyagi782

Iterating Through a List

Lists can be iterated using loops.

Example 1: Using a for loop:

Example 2: Using a while loop:


Yogesh Tyagi
@ytyagi782

List Slicing

List slicing allows you to access a subset


of elements using [start:end:step].

Example

Omitting start starts from the


Key beginning.
Notes Omitting end slices to the end of
the list.
Yogesh Tyagi
@ytyagi782

List Comprehension
List comprehension is a concise way to create
lists using a single line of code.

Syntax

Example

1. Create a list of squares:

2. Create a list of squares:


Yogesh Tyagi
@ytyagi782

Adding Elements to a List

sort() Sorts the list in ascending order (in-place).

sorted() Returns a new sorted list.

reverse() Reverses the list in place.


Yogesh Tyagi
@ytyagi782

Common List Methods


Yogesh Tyagi
@ytyagi782

Nested Lists

Lists can contain other lists, enabling


the creation of multidimensional data
structures.

Example

Use Representing tables, grids, or


Cases: matrices.
Yogesh Tyagi
@ytyagi782

Adding Elements to a List

IndexError Accessing elements outside the list range.

TypeError Using unsupported operations.

ValueError
Trying to remove an element that doesn’t exist.
Yogesh Tyagi
@ytyagi782

Wrap-Up

"Master Lists for Versatile Data


Management in Python"
Lists are a versatile and fundamental data
structure in Python. By mastering list
operations, methods, and comprehensions,
you can efficiently manage and manipulate
collections of data. Practice regularly for
deeper understanding!
Yogesh Tyagi
@ytyagi782

Follow for More

You might also like