Topic 12: What is List in python?
Data Type "List" in Python
In Python, a List is one of the most commonly used and versatile data types. It allows us to store multiple items in a single variable. Lists are ordered, mutable (changeable), and allow duplicate values making them perfect for storing collections of data such as numbers, strings, or even other lists.
A List in Python is defined as a sequence of items enclosed in square brackets [ ], separated by commas.
Syntax:
list_name = [item1, item2, item3, ...]
Example:
fruits = ["apple", "banana", "cherry"]
numbers = [10, 20, 30, 40, 50]
mixed = [10, "hello", 3.14, True]
Characteristics of Python Lists
| Property | Description |
|---|---|
| Ordered | The items have a defined order and can be accessed using an index. |
| Mutable | You can change, add, or remove items after creation. |
| Heterogeneous | A list can store elements of different data types. |
| Allows Duplicates | Lists can contain repeated elements. |
| Dynamic | You can add or remove elements at runtime. |
Accessing Elements in a List
You can access elements using index numbers.
Python uses zero-based indexing, meaning the first element has index 0.
Syntax:
list_name[index]
Example:
fruits = ["apple", "banana", "cherry", "orange"]
print(fruits[0]) # Output: apple
print(fruits[2]) # Output: cherry
Negative Indexing:
You can also access elements from the end using negative indexes.
print(fruits[-1]) # Output: orange
print(fruits[-2]) # Output: cherry
Modifying List Elements
Lists are mutable, so you can change items after creation.
Syntax:
list_name[index] = new_value
Example:
fruits = ["apple", "banana", "cherry"]
fruits[1] = "mango"
print(fruits) # Output: ['apple', 'mango', 'cherry']
Adding Elements to a List
| Method | Description | Example |
|---|---|---|
append() |
Adds a single element to the end | fruits.append("orange") |
insert() |
Adds an element at a specific index | fruits.insert(1, "grapes") |
extend() |
Adds multiple elements (from another list) | fruits.extend(["kiwi", "melon"]) |
Example:
fruits = ["apple", "banana"]
fruits.append("cherry")
fruits.insert(1, "mango")
fruits.extend(["orange", "kiwi"])
print(fruits)
# Output: ['apple', 'mango', 'banana', 'cherry', 'orange', 'kiwi']
Removing Elements from a List
| Method | Description | Example |
|---|---|---|
remove(value) |
Removes first occurrence of a value | fruits.remove("banana") |
pop(index) |
Removes element at a specific index | fruits.pop(2) |
clear() |
Removes all elements | fruits.clear() |
Example:
fruits = ["apple", "banana", "cherry"]
fruits.remove("banana")
print(fruits) # Output: ['apple', 'cherry']
fruits.pop(0)
print(fruits) # Output: ['cherry']
fruits.clear()
print(fruits) # Output: []
List Operations
Python supports several operations with lists.
| Operator | Description | Example |
|---|---|---|
+ |
Concatenation | [1,2] + [3,4] → [1,2,3,4] |
* |
Repetition | [1,2] * 3 → [1,2,1,2,1,2] |
in |
Membership Test | "apple" in fruits → True |
len() |
Length of List | len(fruits) |
max() |
Maximum Value | max(numbers) |
min() |
Minimum Value | min(numbers) |
Looping Through a List
You can iterate through list elements using a for loop.
Example:
fruits = ["apple", "banana", "cherry"]
for fruit in fruits:
print(fruit)
Output:
apple
banana
cherry
List Slicing
Slicing allows you to access a range of elements from a list.
Syntax:
list_name[start:end]
Example:
numbers = [10, 20, 30, 40, 50, 60]
print(numbers[1:4]) # Output: [20, 30, 40]
print(numbers[:3]) # Output: [10, 20, 30]
print(numbers[3:]) # Output: [40, 50, 60]
Useful Built-in List Functions
| Function | Description | Example |
|---|---|---|
len(list) |
Returns number of items | len(fruits) |
sum(list) |
Returns sum of numeric elements | sum(numbers) |
sorted(list) |
Returns a sorted copy | sorted(numbers) |
list.count(x) |
Counts occurrences of element | numbers.count(10) |
list.index(x) |
Returns index of element | numbers.index(20) |
list.reverse() | Reverses the list | numbers.reverse() |
list.sort() |
Sorts the list in ascending order | numbers.sort() |
Visual Representation of List Indexing
List: [10, 20, 30, 40, 50]
Index: 0 1 2 3 4
Neg. Idx: -5 -4 -3 -2 -1
Example Program: Working with Lists
# Python program to demonstrate list operations
numbers = [10, 20, 30, 40, 50]
print("Original List:", numbers)
# Adding elements
numbers.append(60)
numbers.insert(2, 25)
print("After adding:", numbers)
# Removing elements
numbers.remove(40)
numbers.pop(0)
print("After removing:", numbers)
# Accessing and slicing
print("First element:", numbers[0])
print("Sublist:", numbers[1:4])
# Sorting
numbers.sort()
print("Sorted List:", numbers)
Summary
- List is a collection data type used to store multiple items in a single variable.
- Lists are ordered, mutable, and can store different data types.
- You can access, modify, add, or remove items easily.
- Python provides powerful methods like
append(),insert(),remove(),sort(), and more. - Lists make data organization and manipulation simpler and efficient.
Conclusion
👈Pevious Topic "String" List Quiz 👉 active soon......
Comments
Post a Comment