-
Hajipur, Bihar, 844101
In Python, both lists and arrays can be used to store multiple items in a single variable. However, they serve slightly different purposes.
A list is a built-in, flexible data structure that can store any data type — integers, strings, floats, or even other lists.
my_list = [1, 2, 3, "hello", 4.5]
print(my_list)
✅ Output: [1, 2, 3, 'hello', 4.5]
Ordered
Mutable (you can change items)
Allows duplicates
Can store mixed types
print(my_list[0]) # First item
print(my_list[-1]) # Last item
my_list[1] = 99
print(my_list) # [1, 99, 3, 'hello', 4.5]
my_list.append("new") # Add at end
my_list.insert(2, "mid") # Add at index
my_list.remove("hello") # Remove by value
my_list.pop() # Remove last item
Python also has an array module, but it only stores items of the same type (like all integers).
import array
a = array.array('i', [1, 2, 3])
print(a)
✅ 'i'
stands for integer array.
Use arrays for performance and memory efficiency when dealing with large numeric datasets.
Type Code | C Type | Python Type |
---|---|---|
'i' |
int | int |
'f' |
float | float |
'u' |
Py_UNICODE | Unicode |
Feature | List | Array (array module) |
---|---|---|
Data Type | Mixed | Same type only |
Built-in | Yes | Need to import module |
Use Case | General-purpose | Numeric processing |
Performance | Slower | Faster for numbers |
Q1. Write a Python program to create a list of your favorite fruits and print it.
Q2. Write a Python program to replace the 2nd element in a list with a new value.
Q3. Write a Python program to append a new number to a list.
Q4. Write a Python program to use pop()
to remove the last element from a list.
Q5. Write a Python program to check if a value exists in a list using the in
keyword.
Q6. Write a Python program to create an integer array using array.array
.
Q7. Write a Python program to use a loop to print all values in an array.
Q8. Write a Python program to insert a value at index 1 in a list.
Q9. Write a Python program to use remove()
to delete a value from a list.
Q10. Write a Python program to count how many times an element appears in a list using count()
.
Q1: What type of data can a Python list store?
Q2: Which method adds an item at the end of a list?
Q3: What does pop() do in a list?
Q4: What is the index of the first element in a Python list?
Q5: What is a major limitation of Python arrays?
Q6: Which module must be imported to use arrays in Python?
Q7: Which method can be used to insert into a list at a specific index?
Q8: What does list[-1] return?
Q9: Can a list contain another list?
Q10: What will len([10, 20, 30]) return?