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

List_in_Python

Uploaded by

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

List_in_Python

Uploaded by

devansh700singh
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

Lists in Python Programming

Introduction to Lists
What is a List?
•List is a collection of items that are ordered, changeable, and allow duplicate
values.
•Lists are one of the most commonly used data types in Python.
•Lists are written with square brackets.

Example:
fruits = ["apple", "banana", "cherry"]
print(fruits)

Output:
["apple", "banana", "cherry"]
Difference Between List, Tuple and Set
 Key Differences
Feature List Tuple Set

Definition Ordered, changeable, Ordered, unchangeable, Unordered, unchangeable*,


allows duplicates allows duplicates no duplicates

Syntax [] () {}
Mutable? Yes No Partially*
Example fruits = ["apple"] fruits = ("apple") fruits = {"apple"}

*Note: Sets are unchangeable in structure, but their elements can be added or removed.
Various Operations on Lists
Creating a List

You can create a list by placing items (elements) inside


square brackets [], separated by commas.

•Example:
numbers = [1, 2, 3, 4]
print(numbers)
•Output:
[1, 2, 3, 4]
2. Adding Elements to a List

Using append() Using insert() Using extend()


Adds a single element to Adds an element at a Adds multiple elements from
the end of the list. specific position. another list or iterable.
Example: Example: Example:

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


fruits.append("cherry") fruits.insert(1, "orange") fruits.extend(["cherry", "grape"])
print(fruits) print(fruits) print(fruits)
Output: Output: Output:
["apple", "banana", "cherry"] ["apple", "orange", "banana"] ["apple", "banana", "cherry",
"grape"]
3. Deleting Elements from the List

Using remove() Using pop()


Removes the first occurrence of the Removes the element at a specific index
specified value. or the last element if no index is provided.
Example: Example:

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


fruits.remove("banana") fruits.pop(1)
print(fruits) print(fruits)
Output: Output:
["apple", "cherry"] ["apple", "cherry"]
3. Deleting Elements from the List

Using del Using clear()


Deletes an element at a Removes all elements from the list.
specific index or the entire list.
Example: Example:

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


del fruits[1] fruits.clear()
print(fruits) print(fruits)
Output: Output:
["apple", "cherry"] []
4. Modifying or Replacing List Elements
You can change the value of an element by accessing it via
its index.
• Example:
fruits = ["apple", "banana", "cherry"]
fruits[1] = "orange“
print(fruits)
• Output:
["apple", "orange", "cherry"]
5. List Slicing

•Allows you to access a subset of the list.

•Syntax:
list[start:end] (end index is exclusive).
•Example 1:
fruits = ["apple", "banana", "cherry", "grape"]
print(fruits[1:3])
• Output:
["banana", "cherry"]
5. List Slicing
•Example 2:
fruits = ["apple", "banana", "cherry", "grape"]
print(fruits[-3:-1])
• Output:
['banana', 'cherry’]
•Example 3:
fruits = ["apple", "banana", "cherry", "grape"]
print(fruits[1:])
• Output:
['banana', 'cherry', 'grape’]
5. List Slicing
• Example 4:
fruits = ["apple", "banana", "cherry", "grape"]
print(fruits[:1])
• Output:
[‘apple’]
• Example 5:
fruits = ["apple", "banana", "cherry", "grape"]
print(fruits[:])
• Output:
[‘apple’, 'banana', 'cherry', 'grape’]
6. Traversing a List

•You can iterate through a list using a for loop.


•Example:
fruits = ["apple", "banana", "cherry"]
for element in fruits:
print(element)
•Output
apple
banana
cherry
Practice Questions
ALL DONE

1. Create a list of your favorite movies and add two more using append().
2. Remove a movie from the list using remove().
3. Replace one movie with another of your choice.
4. Print the list in reverse order using slicing.
5. Traverse the list and print each movie in uppercase.

You might also like