A list in Python is an ordered, mutable (changeable), and iterable collection used to store multiple items in a single variable. Lists can contain elements of different data types (integers, strings, floats, even other lists).
* Ordered – Items are stored in a specific order and can be accessed using an index.
* Mutable – You can modify a list by adding, removing, or changing elements.
* Allows Duplicates – Lists can contain duplicate values.
* Heterogeneous – A list can store different data types (integers, strings, lists, etc.).
* Dynamic Size – Lists can grow or shrink dynamically as elements are added or removed.
my_list = []
fruits = ["apple", "banana", "cherry"]
numbers = [1, 2, 3, 4, 5]
mixed_list = [1, "hello", 3.14, True]
fruits = ["apple", "banana", "cherry"]
print(fruits[0]) # Output: apple
print(fruits[-1]) # Output: cherry (negative index counts from the end)
Using Slicing :
numbers = [10, 20, 30, 40, 50]
print(numbers[1:4]) # Output: [20, 30, 40]
print(numbers[:3]) # Output: [10, 20, 30]
print(numbers[-3:]) # Output: [30, 40, 50]
fruits.append("orange") # Adds at the end
fruits.insert(1, "grape") # Inserts at index 1
print(fruits) # Output: ['apple', 'grape', 'banana', 'cherry', 'orange']?
fruits.remove("banana") # Removes "banana"
popped_item = fruits.pop(2) # Removes element at index 2
del fruits[0] # Deletes first element
fruits.clear() # Empties the list?
fruits[1] = "blueberry" # Change "banana" to "blueberry"?
for fruit in fruits:
print(fruit)?
squared_numbers = [x**2 for x in [1, 2, 3, 4, 5]]
print(squared_numbers) # Output: [1, 4, 9, 16, 25]?
| Method | Description | Example |
|---|---|---|
append(x) |
Adds an element to the end | list.append(10) |
insert(i, x) |
Inserts an element at index i |
list.insert(1, 20) |
remove(x) |
Removes first occurrence of x |
list.remove(5) |
pop(i) |
Removes element at index i |
list.pop(2) |
clear() |
Removes all elements | list.clear() |
sort() |
Sorts the list (ascending by default) | list.sort() |
reverse() |
Reverses the list order | list.reverse() |
count(x) |
Counts occurrences of x |
list.count(2) |
index(x) |
Returns index of first occurrence of x |
list.index(3) |
students = ["Alice", "Bob", "Charlie"]
students.append("David")
students.remove("Bob")
print(students) # Output: ['Alice', 'Charlie', 'David']?A tuple in Python is an ordered, immutable (unchangeable), and iterable collection used to store multiple items in a single variable. Unlike lists, tuples cannot be modified after creation, making them useful for storing fixed data.
* Ordered – Items maintain a specific sequence and can be accessed via indexing.
* Immutable – Once created, elements cannot be changed, added, or removed.
* Allows Duplicates – Can store repeated values.
* Heterogeneous – Can store multiple data types (integers, strings, lists, etc.).
* Memory Efficient & Faster – Uses less memory and is faster than lists due to immutability.
tuple1 = (1, 2, 3)
tuple2 = (4, 5, 6)
merged_tuple = tuple1 + tuple2
print(merged_tuple) # Output: (1, 2, 3, 4, 5, 6)?
numbers = (1, 2, 3)
repeated = numbers * 3
print(repeated) # Output: (1, 2, 3, 1, 2, 3, 1, 2, 3)?
person = ("John", 30, "Engineer")
name, age, profession = person
print(name) # Output: John
print(age) # Output: 30
print(profession) # Output: Engineer?
| Method | Description | Examp
|
|---|
| Feature | List | Tuple |
|---|---|---|
| Mutability | Mutable (can be modified) ? | Immutable (cannot be modified after creation) ? |
| Syntax | Defined using square brackets [ ] |
Defined using parentheses ( ) |
| Performance | Slower due to dynamic resizing and modification overhead ? | Faster due to immutability and fixed structure ? |
| Memory Usage | Takes more memory ? | Takes less memory due to immutability ? |
| Operations | Suppor
Note : This article is only for students, for the purpose of enhancing their knowledge. This article is collected from several websites, the copyrights of this article also belong to those websites like : Newscientist, Techgig, simplilearn, scitechdaily, TechCrunch, TheVerge etc,.
|