Python Programming for Beginners

Chapter: Python Lists – Creating, Accessing, Modifying and Using Lists

<h2>12.1 Introduction to Python Lists</h2>

<p>
A list is a collection used to store multiple values in a single variable.
Lists are one of the most commonly used data structures in Python.
</p>

<p>
A list can contain numbers, strings, Boolean values, or even other lists.
Lists are ordered, changeable, and allow duplicate values.
</p>

<pre><code>fruits = ["Apple", "Banana", "Mango"]

print(fruits)</code></pre>

<p>Output:</p>

<pre><code>['Apple', 'Banana', 'Mango']</code></pre>


<h2>12.2 Creating a List</h2>

<p>
Lists are created by placing values inside square brackets
<code>[]</code>.
</p>

<pre><code>numbers = [10, 20, 30, 40, 50]

names = ["Aman", "Ravi", "Neha"]

print(numbers)
print(names)</code></pre>


<h2>12.3 Creating an Empty List</h2>

<p>
An empty list contains no elements. It can be created using empty
square brackets.
</p>

<pre><code>students = []

print(students)</code></pre>

<p>Output:</p>

<pre><code>[]</code></pre>


<h2>12.4 Lists with Different Data Types</h2>

<p>
A Python list can contain values of different data types.
</p>

<pre><code>student = ["Aman", 16, 85.5, True]

print(student)</code></pre>

<p>Output:</p>

<pre><code>['Aman', 16, 85.5, True]</code></pre>


<h2>12.5 Accessing List Elements</h2>

<p>
List elements can be accessed using their index. Python uses zero-based
indexing, so the first element has index <code>0</code>.
</p>

<pre><code>fruits = ["Apple", "Banana", "Mango"]

print(fruits[0])
print(fruits[1])
print(fruits[2])</code></pre>

<p>Output:</p>

<pre><code>Apple
Banana
Mango</code></pre>


<h2>12.6 Negative Indexing</h2>

<p>
Negative indexes allow you to access elements from the end of a list.
The last element has index <code>-1</code>.
</p>

<pre><code>fruits = ["Apple", "Banana", "Mango", "Orange"]

print(fruits[-1])
print(fruits[-2])</code></pre>

<p>Output:</p>

<pre><code>Orange
Mango</code></pre>


<h2>12.7 Changing List Elements</h2>

<p>
Lists are mutable, which means their elements can be changed after the
list has been created.
</p>

<pre><code>fruits = ["Apple", "Banana", "Mango"]

fruits[1] = "Orange"

print(fruits)</code></pre>

<p>Output:</p>

<pre><code>['Apple', 'Orange', 'Mango']</code></pre>


<h2>12.8 Changing Multiple Elements</h2>

<p>
You can replace multiple elements by assigning a new list to a slice.
</p>

<pre><code>numbers = [10, 20, 30, 40, 50]

numbers[1:3] = [200, 300]

print(numbers)</code></pre>

<p>Output:</p>

<pre><code>[10, 200, 300, 40, 50]</code></pre>


<h2>12.9 Finding the Length of a List</h2>

<p>
The <code>len()</code> function returns the number of elements in a list.
</p>

<pre><code>fruits = ["Apple", "Banana", "Mango"]

print(len(fruits))</code></pre>

<p>Output:</p>

<pre><code>3</code></pre>


<h2>12.10 List Slicing</h2>

<p>
List slicing is used to extract a portion of a list.
</p>

<pre><code>numbers = [10, 20, 30, 40, 50]

print(numbers[1:4])</code></pre>

<p>Output:</p>

<pre><code>[20, 30, 40]</code></pre>


<h2>12.11 Slicing from the Beginning</h2>

<pre><code>numbers = [10, 20, 30, 40, 50]

print(numbers[:3])</code></pre>

<p>Output:</p>

<pre><code>[10, 20, 30]</code></pre>


<h2>12.12 Slicing to the End</h2>

<pre><code>numbers = [10, 20, 30, 40, 50]

print(numbers[2:])</code></pre>

<p>Output:</p>

<pre><code>[30, 40, 50]</code></pre>


<h2>12.13 Slicing with a Step</h2>

<p>
A third value can be used to specify the step while slicing.
</p>

<pre><code>numbers = [10, 20, 30, 40, 50, 60]

print(numbers[0:6:2])</code></pre>

<p>Output:</p>

<pre><code>[10, 30, 50]</code></pre>


<h2>12.14 Reversing a List Using Slicing</h2>

<pre><code>numbers = [10, 20, 30, 40, 50]

print(numbers[::-1])</code></pre>

<p>Output:</p>

<pre><code>[50, 40, 30, 20, 10]</code></pre>


<h2>12.15 Adding an Element with append()</h2>

<p>
The <code>append()</code> method adds one element to the end of a list.
</p>

<pre><code>fruits = ["Apple", "Banana"]

fruits.append("Mango")

print(fruits)</code></pre>

<p>Output:</p>

<pre><code>['Apple', 'Banana', 'Mango']</code></pre>


<h2>12.16 Adding an Element at a Specific Position</h2>

<p>
The <code>insert()</code> method adds an element at a specified index.
</p>

<pre><code>fruits = ["Apple", "Mango"]

fruits.insert(1, "Banana")

print(fruits)</code></pre>

<p>Output:</p>

<pre><code>['Apple', 'Banana', 'Mango']</code></pre>


<h2>12.17 Adding Multiple Elements with extend()</h2>

<p>
The <code>extend()</code> method adds multiple elements to the end of
a list.
</p>

<pre><code>fruits = ["Apple", "Banana"]

more_fruits = ["Mango", "Orange"]

fruits.extend(more_fruits)

print(fruits)</code></pre>

<p>Output:</p>

<pre><code>['Apple', 'Banana', 'Mango', 'Orange']</code></pre>


<h2>12.18 Removing an Element with remove()</h2>

<p>
The <code>remove()</code> method removes the first matching element
from a list.
</p>

<pre><code>fruits = ["Apple", "Banana", "Mango"]

fruits.remove("Banana")

print(fruits)</code></pre>

<p>Output:</p>

<pre><code>['Apple', 'Mango']</code></pre>


<h2>12.19 Removing an Element with pop()</h2>

<p>
The <code>pop()</code> method removes an element using its index.
If no index is provided, it removes the last element.
</p>

<pre><code>fruits = ["Apple", "Banana", "Mango"]

removed = fruits.pop(1)

print("Removed:", removed)
print(fruits)</code></pre>

<p>Output:</p>

<pre><code>Removed: Banana
['Apple', 'Mango']</code></pre>


<h2>12.20 Removing the Last Element</h2>

<pre><code>numbers = [10, 20, 30, 40]

numbers.pop()

print(numbers)</code></pre>

<p>Output:</p>

<pre><code>[10, 20, 30]</code></pre>


<h2>12.21 Deleting an Element with del</h2>

<p>
The <code>del</code> statement can be used to delete an element or
a section of a list.
</p>

<pre><code>numbers = [10, 20, 30, 40]

del numbers[1]

print(numbers)</code></pre>

<p>Output:</p>

<pre><code>[10, 30, 40]</code></pre>


<h2>12.22 Clearing a List</h2>

<p>
The <code>clear()</code> method removes all elements from a list.
</p>

<pre><code>numbers = [10, 20, 30, 40]

numbers.clear()

print(numbers)</code></pre>

<p>Output:</p>

<pre><code>[]</code></pre>


<h2>12.23 Checking Whether an Element Exists</h2>

<p>
The <code>in</code> operator can be used to check whether a value exists
in a list.
</p>

<pre><code>fruits = ["Apple", "Banana", "Mango"]

print("Banana" in fruits)
print("Orange" in fruits)</code></pre>

<p>Output:</p>

<pre><code>True
False</code></pre>


<h2>12.24 Checking Whether an Element Does Not Exist</h2>

<pre><code>fruits = ["Apple", "Banana", "Mango"]

print("Orange" not in fruits)</code></pre>

<p>Output:</p>

<pre><code>True</code></pre>


<h2>12.25 Finding an Element with index()</h2>

<p>
The <code>index()</code> method returns the position of the first
matching element.
</p>

<pre><code>fruits = ["Apple", "Banana", "Mango"]

position = fruits.index("Mango")

print(position)</code></pre>

<p>Output:</p>

<pre><code>2</code></pre>


<h2>12.26 Counting Elements with count()</h2>

<p>
The <code>count()</code> method returns how many times a value occurs
in a list.
</p>

<pre><code>numbers = [10, 20, 10, 30, 10]

print(numbers.count(10))</code></pre>

<p>Output:</p>

<pre><code>3</code></pre>


<h2>12.27 Sorting a List</h2>

<p>
The <code>sort()</code> method arranges list elements in ascending
order by default.
</p>

<pre><code>numbers = [50, 20, 40, 10, 30]

numbers.sort()

print(numbers)</code></pre>

<p>Output:</p>

<pre><code>[10, 20, 30, 40, 50]</code></pre>


<h2>12.28 Sorting in Descending Order</h2>

<pre><code>numbers = [50, 20, 40, 10, 30]

numbers.sort(reverse=True)

print(numbers)</code></pre>

<p>Output:</p>

<pre><code>[50, 40, 30, 20, 10]</code></pre>


<h2>12.29 Reversing a List with reverse()</h2>

<p>
The <code>reverse()</code> method reverses the order of elements in
the existing list.
</p>

<pre><code>numbers = [10, 20, 30, 40]

numbers.reverse()

print(numbers)</code></pre>

<p>Output:</p>

<pre><code>[40, 30, 20, 10]</code></pre>


<h2>12.30 Copying a List</h2>

<p>
The <code>copy()</code> method creates a separate copy of a list.
</p>

<pre><code>numbers = [10, 20, 30]

new_numbers = numbers.copy()

print(new_numbers)</code></pre>

<p>Output:</p>

<pre><code>[10, 20, 30]</code></pre>


<h2>12.31 Copying a List with list()</h2>

<p>
The <code>list()</code> function can also be used to create a copy.
</p>

<pre><code>numbers = [10, 20, 30]

new_numbers = list(numbers)

print(new_numbers)</code></pre>


<h2>12.32 Joining Two Lists</h2>

<p>
The <code>+</code> operator can be used to combine two lists.
</p>

<pre><code>list1 = [10, 20, 30]
list2 = [40, 50, 60]

combined = list1 + list2

print(combined)</code></pre>

<p>Output:</p>

<pre><code>[10, 20, 30, 40, 50, 60]</code></pre>


<h2>12.33 Repeating a List</h2>

<p>
The multiplication operator can repeat the elements of a list.
</p>

<pre><code>numbers = [1, 2]

print(numbers * 3)</code></pre>

<p>Output:</p>

<pre><code>[1, 2, 1, 2, 1, 2]</code></pre>


<h2>12.34 Looping Through a List</h2>

<p>
A <code>for</code> loop can be used to process each element in a list.
</p>

<pre><code>fruits = ["Apple", "Banana", "Mango"]

for fruit in fruits:
   print(fruit)</code></pre>

<p>Output:</p>

<pre><code>Apple
Banana
Mango</code></pre>


<h2>12.35 Looping Through a List Using Index</h2>

<pre><code>fruits = ["Apple", "Banana", "Mango"]

for i in range(len(fruits)):
   print(i, fruits[i])</code></pre>

<p>Output:</p>

<pre><code>0 Apple
1 Banana
2 Mango</code></pre>


<h2>12.36 Using enumerate()</h2>

<p>
The <code>enumerate()</code> function provides both the index and
the value while looping through a list.
</p>

<pre><code>fruits = ["Apple", "Banana", "Mango"]

for index, fruit in enumerate(fruits):
   print(index, fruit)</code></pre>

<p>Output:</p>

<pre><code>0 Apple
1 Banana
2 Mango</code></pre>


<h2>12.37 Finding the Largest Value</h2>

<p>
The <code>max()</code> function returns the largest value in a list.
</p>

<pre><code>numbers = [15, 8, 42, 23, 10]

print(max(numbers))</code></pre>

<p>Output:</p>

<pre><code>42</code></pre>


<h2>12.38 Finding the Smallest Value</h2>

<pre><code>numbers = [15, 8, 42, 23, 10]

print(min(numbers))</code></pre>

<p>Output:</p>

<pre><code>8</code></pre>


<h2>12.39 Calculating the Sum</h2>

<p>
The <code>sum()</code> function calculates the total of numeric
elements in a list.
</p>

<pre><code>numbers = [10, 20, 30, 40]

print(sum(numbers))</code></pre>

<p>Output:</p>

<pre><code>100</code></pre>


<h2>12.40 Creating a List from range()</h2>

<p>
The <code>list()</code> function can be combined with
<code>range()</code> to create a list of numbers.
</p>

<pre><code>numbers = list(range(1, 6))

print(numbers)</code></pre>

<p>Output:</p>

<pre><code>[1, 2, 3, 4, 5]</code></pre>


<h2>12.41 List Comprehension</h2>

<p>
List comprehension provides a short way to create a new list from
an existing iterable.
</p>

<pre><code>squares = [number ** 2 for number in range(1, 6)]

print(squares)</code></pre>

<p>Output:</p>

<pre><code>[1, 4, 9, 16, 25]</code></pre>


<h2>12.42 List Comprehension with a Condition</h2>

<p>
A condition can be included in a list comprehension.
</p>

<pre><code>numbers = range(1, 11)

even_numbers = [
   number
   for number in numbers
   if number % 2 == 0
]

print(even_numbers)</code></pre>

<p>Output:</p>

<pre><code>[2, 4, 6, 8, 10]</code></pre>


<h2>12.43 Creating a List of Squares</h2>

<pre><code>numbers = [1, 2, 3, 4, 5]

squares = []

for number in numbers:
   squares.append(number ** 2)

print(squares)</code></pre>

<p>Output:</p>

<pre><code>[1, 4, 9, 16, 25]</code></pre>


<h2>12.44 Nested Lists</h2>

<p>
A list can contain other lists. Such a structure is called a nested
list.
</p>

<pre><code>students = [
   ["Aman", 85],
   ["Ravi", 90],
   ["Neha", 88]
]

print(students)</code></pre>


<h2>12.45 Accessing Nested List Elements</h2>

<pre><code>students = [
   ["Aman", 85],
   ["Ravi", 90],
   ["Neha", 88]
]

print(students[0][0])
print(students[0][1])</code></pre>

<p>Output:</p>

<pre><code>Aman
85</code></pre>


<h2>12.46 Updating a Nested List</h2>

<pre><code>students = [
   ["Aman", 85],
   ["Ravi", 90]
]

students[0][1] = 95

print(students)</code></pre>

<p>Output:</p>

<pre><code>[['Aman', 95], ['Ravi', 90]]</code></pre>


<h2>12.47 List of Strings</h2>

<pre><code>cities = ["Delhi", "Gwalior", "Bhopal", "Indore"]

for city in cities:
   print(city)</code></pre>


<h2>12.48 List of Student Marks</h2>

<pre><code>marks = [78, 85, 92, 67, 88]

print("Total:", sum(marks))
print("Highest:", max(marks))
print("Lowest:", min(marks))
print("Average:", sum(marks) / len(marks))</code></pre>


<h2>12.49 Example: Find Even Numbers</h2>

<pre><code>numbers = [10, 15, 22, 31, 40, 55]

for number in numbers:
   if number % 2 == 0:
       print(number)</code></pre>

<p>Output:</p>

<pre><code>10
22
40</code></pre>


<h2>12.50 Example: Find Numbers Greater Than 50</h2>

<pre><code>numbers = [25, 60, 45, 80, 35, 90]

for number in numbers:
   if number > 50:
       print(number)</code></pre>

<p>Output:</p>

<pre><code>60
80
90</code></pre>


<h2>12.51 Example: Remove Duplicate Values</h2>

<p>
A set can be used to remove duplicate values from a list.
</p>

<pre><code>numbers = [10, 20, 10, 30, 20, 40]

unique_numbers = list(set(numbers))

print(unique_numbers)</code></pre>

<p>
Note: Sets do not guarantee the original list order in the general
case.
</p>


<h2>12.52 Example: Shopping List</h2>

<pre><code>shopping = []

shopping.append("Milk")
shopping.append("Bread")
shopping.append("Rice")

print("Shopping List:")

for item in shopping:
   print("-", item)</code></pre>


<h2>12.53 Example: Student Marks Analysis</h2>

<pre><code>marks = [78, 85, 92, 67, 88]

highest = max(marks)
lowest = min(marks)
total = sum(marks)
average = total / len(marks)

print("Highest:", highest)
print("Lowest:", lowest)
print("Total:", total)
print("Average:", average)</code></pre>


<h2>12.54 Common List Methods</h2>

<table>
   <thead>
       <tr>
           <th>Method</th>
           <th>Purpose</th>
       </tr>
   </thead>
   <tbody>
       <tr>
           <td><code>append()</code></td>
           <td>Adds an element to the end</td>
       </tr>
       <tr>
           <td><code>insert()</code></td>
           <td>Adds an element at a specific position</td>
       </tr>
       <tr>
           <td><code>extend()</code></td>
           <td>Adds multiple elements</td>
       </tr>
       <tr>
           <td><code>remove()</code></td>
           <td>Removes a matching value</td>
       </tr>
       <tr>
           <td><code>pop()</code></td>
           <td>Removes an element by index</td>
       </tr>
       <tr>
           <td><code>clear()</code></td>
           <td>Removes all elements</td>
       </tr>
       <tr>
           <td><code>index()</code></td>
           <td>Finds the position of a value</td>
       </tr>
       <tr>
           <td><code>count()</code></td>
           <td>Counts occurrences</td>
       </tr>
       <tr>
           <td><code>sort()</code></td>
           <td>Sorts the list</td>
       </tr>
       <tr>
           <td><code>reverse()</code></td>
           <td>Reverses the list</td>
       </tr>
       <tr>
           <td><code>copy()</code></td>
           <td>Creates a copy of the list</td>
       </tr>
   </tbody>
</table>


<h2>12.55 List vs Tuple</h2>

<table>
   <thead>
       <tr>
           <th>Feature</th>
           <th>List</th>
           <th>Tuple</th>
       </tr>
   </thead>
   <tbody>
       <tr>
           <td>Syntax</td>
           <td><code>[]</code></td>
           <td><code>()</code></td>
       </tr>
       <tr>
           <td>Changeable</td>
           <td>Yes</td>
           <td>No</td>
       </tr>
       <tr>
           <td>Ordered</td>
           <td>Yes</td>
           <td>Yes</td>
       </tr>
       <tr>
           <td>Duplicates</td>
           <td>Allowed</td>
           <td>Allowed</td>
       </tr>
   </tbody>
</table>


<h2>12.56 Chapter Summary</h2>

<p>
In this chapter, you learned how to create and work with Python lists.
You learned how to access, update, add, remove, search, sort and
process list elements.
</p>

<ul>
   <li>Creating lists</li>
   <li>Accessing list elements</li>
   <li>Positive and negative indexing</li>
   <li>List slicing</li>
   <li>Adding elements</li>
   <li>Removing elements</li>
   <li>Sorting and reversing lists</li>
   <li>Copying lists</li>
   <li>Looping through lists</li>
   <li>Nested lists</li>
   <li>List comprehension</li>
   <li>Useful list functions and methods</li>
</ul>


<h2>12.57 Quick Revision Questions</h2>

<ol>
   <li>What is a list in Python?</li>
   <li>How do you create an empty list?</li>
   <li>What is the first index of a list?</li>
   <li>How do you add an element to the end of a list?</li>
   <li>What is the difference between remove() and pop()?</li>
   <li>How do you sort a list?</li>
   <li>What is list slicing?</li>
   <li>What is a nested list?</li>
   <li>What is list comprehension?</li>
   <li>How can you find the largest value in a list?</li>
</ol>


<h2>12.58 Practice Exercises</h2>

<ol>
   <li>Create a list containing five student names.</li>
   <li>Add two more names to the list.</li>
   <li>Remove one name from the list.</li>
   <li>Print the first and last elements.</li>
   <li>Sort a list of numbers in ascending order.</li>
   <li>Sort a list of numbers in descending order.</li>
   <li>Find the largest and smallest number.</li>
   <li>Calculate the total and average of student marks.</li>
   <li>Create a list containing the squares of numbers from 1 to 10.</li>
   <li>Create a list containing only the even numbers from 1 to 20.</li>
</ol>


<h2>12.59 Mini Project: Student Marks Manager</h2>

<p>
Create a simple program that stores student marks in a list and
calculates the total, average, highest and lowest marks.
</p>

<pre><code>marks = [78, 85, 92, 67, 88]

print("Marks:", marks)
print("Total:", sum(marks))
print("Average:", sum(marks) / len(marks))
print("Highest:", max(marks))
print("Lowest:", min(marks))</code></pre>


<h2>What's Next?</h2>

<p>
<strong>
Next Chapter: Python Dictionaries – Creating, Accessing, Updating
and Working with Key-Value Pairs
</strong>
</p>