Python Essentials: a Fast Track to Key Concepts — Chapter 6: Lists

Sajjad Hadi
3 min readJun 24, 2023

Lists are versatile data structures in Python that allow us to store and manipulate collections of items. They can contain elements of different types and provide various operations to work with the data efficiently. In this article, we will dive into the world of lists, covering their creation, accessing elements, list modification, and list slicing. Through real-world examples, you’ll gain a solid understanding of how to leverage lists in your programming projects.

Chapters of This Series

  1. Chapter 1: Variable Basics
  2. Chapter 2: Numbers
  3. Chapter 3: Strings
  4. Chapter 4: String Methods
  5. Chapter 5: Booleans
  6. Chapter 6: Lists

1. List Creation

Creating a list is simple. We can directly assign values to a list using square brackets. Let's consider an example where we create a list of fruits:

fruits = ["apple", "banana", "orange", "kiwi"]
print(fruits)

Output:

["apple", "banana", "orange", "kiwi"]

In this example, we create a list called fruits that contains four elements. We then print the list to verify its contents.

2. Accessing Elements

To access individual elements in a list, we can use their indices. Indices start from 0 for the first element and increment by 1 for each subsequent element. Let’s see an example:

fruits = ["apple", "banana", "orange", "kiwi"]
print(fruits[0]) # Accessing the first element
print(fruits[2]) # Accessing the third element

Output:

"apple"
"orange"

In this code snippet, we access the first and third elements of the fruits list by using their respective indices.

3. Negative Indexing

Another way to access list elements is by using negative indexing. Negative indices allow us to access elements from the end of the list. Consider the following example:

fruits = ["apple", "banana", "orange", "kiwi"]
print(fruits[-1]) # Accessing the last element
print(fruits[-3]) # Accessing the third-to-last element

Output:

"kiwi"
"banana"

In this case, we access the last and third-to-last elements using negative indices.

4. List Modification

Lists are mutable, meaning we can modify their elements. We can assign new values to specific indices or use list methods to modify the list in various ways. Let’s explore an example where we change an element in the list:

fruits = ["apple", "banana", "orange", "kiwi"]
fruits[1] = "grape" # Modifying the second element
print(fruits)

Output:

["apple", "grape", "orange", "kiwi"]

In this code, we modify the second element of the fruits list by assigning a new value to its index.

5. List Slicing

List slicing allows us to extract a portion of a list by specifying start and end indices. This operation creates a new list containing the selected elements. Let’s see an example of list slicing:

numbers = [1, 2, 3, 4, 5]
sliced_numbers = numbers[1:4] # Slicing elements from index 1 to 3
print(sliced_numbers)

Output:

[2, 3, 4]

In this code snippet, we slice a portion of the numbers list from index 1 to 3 (excluding the element at index 4) and store it in a new list called sliced_numbers.

!! Pay Attention !!

List slicing, in particular, plays a vital role in data science and machine learning tasks. It allows us to extract specific subsets of data from larger lists, enabling us to analyze and manipulate data more efficiently. The general formula for list slicing in Python is as follows:

sliced_list = original_list[start:end:step]
  • start: The index where the slice starts (inclusive).
  • end: The index where the slice ends (exclusive).
  • step: The step size or the number of elements to skip (optional).

6. Conclusion

In this chapter, we have explored the different aspects of lists, providing real-world examples and printing the outputs to demonstrate their functionalities. By practicing and experimenting with lists, you will further enhance your Python programming skills and broaden your ability to handle diverse data collections.

Stay tuned for the next part of our series, where we will explore more advanced list operations, such as list methods, appending and removing elements, and list comprehension, to unleash the full potential of lists in Python.

If you found this course helpful and would like to explore more free courses, I invite you to follow my account on Medium and connect with me on LinkedIn. I regularly share valuable content on these platforms.

--

--