Python Lists


A list in Python is used to store multiple values in a single variable.
Lists are ordered, changeable, and allow duplicate values.


🔹 Create a List

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

🔹 List Indexing

print(fruits[0])  # apple
print(fruits[1])  # banana

🔹 Negative Indexing

print(fruits[-1])  # cherry

🔹 List Slicing

print(fruits[1:3])   # ['banana', 'cherry']
print(fruits[:2])    # ['apple', 'banana']

🔹 Change Item Value

fruits[1] = "orange"
print(fruits)  # ['apple', 'orange', 'cherry']

🔹 Loop Through a List

for fruit in fruits:
    print(fruit)

🔹 Check if Item Exists

if "banana" in fruits:
    print("Yes")

🔹 List Length

print(len(fruits))  # 3

🔹 Add Items

fruits.append("mango")      # add to end
fruits.insert(1, "grape")   # insert at position 1

🔹 Remove Items

fruits.remove("banana")  # remove by value
fruits.pop()             # remove last item
del fruits[0]            # remove by index

🔹 Copy a List

new_list = fruits.copy()

🔹 Combine Lists

list1 = ["a", "b"]
list2 = [1, 2]
list3 = list1 + list2

🧪 Try It Yourself

Practice adding, removing, looping, slicing, and checking items in lists.


Practice Questions

Q1. Write a Python program to create a list of 5 countries and print them.

Q2. Write a Python program to print the second and last items in a list.

Q3. Write a Python program to change the third item of a list to "India".

Q4. Write a Python program to add "Python" to the end of a list using append().

Q5. Write a Python program to insert "Java" at index 2 of a list.

Q6. Write a Python program to remove "C++" from a list using remove().

Q7. Write a Python program to copy one list into another using the copy() method.

Q8. Write a Python program to check if "HTML" exists in a list using the in keyword.

Q9. Write a Python program to print all items in a list using a loop.

Q10. Write a Python program to join two lists: one with strings and one with numbers, and print the result.


Python Lists Quiz

Q1: What is a list in Python?

A. Group of variables
B. Collection of unordered data
C. Ordered collection of items
D. Single value holder

Q2: What is the index of the first item in a list?

A. 1
B. 0
C. -1
D. None

Q3: Which method adds an item to the end of a list?

A. insert()
B. add()
C. append()
D. push()

Q4: What does pop() do?

A. Adds item
B. Removes first item
C. Removes last item
D. Removes all items

Q5: How do you get the number of items in a list?

A. count()
B. size()
C. len()
D. number()

Q6: Which method creates a full copy of a list?

A. clone()
B. copy()
C. paste()
D. insert()

Q7: What will fruits[-1] return?

A. First item
B. Last item
C. Error
D. Middle item

Q8: What does del fruits[0] do?

A. Deletes the list
B. Deletes the last item
C. Deletes the first item
D. Nothing

Q9: Can lists contain mixed data types?

A. Yes
B. No
C. Only strings
D. Only numbers

Q10: Which of the following joins two lists?

A. list1 * list2
B. list1.append(list2)
C. list1 + list2
D. list1.add(list2)

Go Back Top