Learn Python with me : Lists
Understanding Python Lists

Understanding Python Lists

Explanation:

In Python, a list is a collection of items that are ordered and changeable. Lists are written with square brackets, and they can contain items of any data type.

Lists are very useful for storing multiple items in a single variable and for performing operations on collections of data. Here's how you can create and work with lists in Python.

Creating a List:

 my_list = [1, 2, 3, 4, 5] 

Accessing List Items:

You can access items in a list by referring to their index number. Note that the first item has an index of 0.

 print(my_list[0])  # Output: 1
print(my_list[2])  # Output: 3 

Modifying List Items:

You can change the value of a specific item by referring to its index.

 my_list[1] = 20
print(my_list)  # Output: [1, 20, 3, 4, 5] 

Adding Items to a List:

You can use the append() method to add an item to the end of a list, or the insert() method to add an item at a specified position.

 my_list.append(6)
print(my_list)  # Output: [1, 20, 3, 4, 5, 6]
my_list.insert(1, 15)
print(my_list)  # Output: [1, 15, 20, 3, 4, 5, 6] 

Removing Items from a List:

You can use the remove() method to remove a specific item, or the pop() method to remove an item at a specified index (or the last item if no index is specified).

 my_list.remove(20)
print(my_list)  # Output: [1, 15, 3, 4, 5, 6]
my_list.pop(2)
print(my_list)  # Output: [1, 15, 4, 5, 6] 

Looping Through a List:

You can loop through the items in a list using a for loop.

 for item in my_list:
    print(item) 

Real-life Example:

Imagine you are organizing a birthday party and you want to keep track of the guests who have RSVPed. You can use a list to store the names of the guests.

 guests = ["Alice", "Bob", "Charlie", "David"]
# Adding a new guest
guests.append("Eve")
# Removing a guest
guests.remove("Charlie")
# Checking the guest list
for guest in guests:
    print(guest) 

Sample Programs:

Easy Level:

Print all items in a list:
 fruits = ["apple", "banana", "cherry"]
for fruit in fruits:
    print(fruit) 
Find the length of a list:
 numbers = [1, 2, 3, 4, 5]
print(len(numbers))  # Output: 5 

Intermediate Level:

Sum all the numbers in a list:
 numbers = [1, 2, 3, 4, 5]
total = 0
for number in numbers:
    total += number
print(total)  # Output: 15 
Find the maximum number in a list:
 numbers = [1, 2, 3, 4, 5]
max_number = numbers[0]
for number in numbers:
    if number > max_number:
        max_number = number
print(max_number)  # Output: 5 

Expert Level:

Sort a list in ascending order:
 numbers = [5, 2, 9, 1, 5, 6]
numbers.sort()
print(numbers)  # Output: [1, 2, 5, 5, 6, 9] 
Merge two lists:
 list1 = [1, 2, 3]
list2 = [4, 5, 6]
merged_list = list1 + list2
print(merged_list)  # Output: [1, 2, 3, 4, 5, 6] 

Homework Questions:

  1. Create a list of your favorite movies and print each movie.
  2. Write a program to find the second largest number in a list.
  3. Write a program to count the number of times an item appears in a list.
  4. Write a program to remove all duplicate items from a list.
  5. Create a list of numbers and write a program to print only the even numbers.
  6. Write a program to find the common items between two lists.
  7. Write a program to reverse the order of items in a list.
  8. Write a program to find the sum of squares of all numbers in a list.
  9. Create a list of tuples containing the name and age of several people. Write a program to sort the list by age.
  10. Write a program to flatten a list of lists.

Learn Python with me : Lists