coding180 icon
Coding180
Lists in Python: Creation, Indexing, Slicing, Adding and Removing Elements, Sorting, Looping Through

Lists in Python: Creation, Indexing, Slicing, Adding and Removing Elements, Sorting, Looping Through

Home > Tutorials > Python for beginners > Data structure


In Python programming, a list is a collection of ordered elements enclosed in square brackets []. Lists can hold any data type such as numbers, strings, booleans, and even other lists. Lists are one of the most frequently used data structures in Python.

This lesson will cover the following topics:

  • Creating Lists
  • Indexing and Slicing Lists
  • Adding and Removing Elements from Lists
  • Sorting Lists
  • Looping Through Lists

Creating Lists

To create a list in Python, you enclose the list elements with square brackets [] separated by commas. Here is an example of a list of integers:

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

 

You can also create a list of strings:

my_string_list = ["apple", "banana", "cherry"]

Or even a mixed list:

mixed_list = [1, "apple", True, 3.14]

 

Indexing and Slicing Lists

Indexing and slicing are methods used to access specific elements of a list. Indexing allows you to access the value of an element based on its position in the list. The index starts at 0 for the first element and increases by one for each subsequent element.

Here is an example of indexing a list to retrieve the first element:

my_list = [1, 2, 3, 4, 5]
first_element = my_list[0]
print(first_element)

Output:

1

Slicing a list extracts a range of elements from the list. The syntax for slicing is list[start:stop], where the start index is inclusive and the stop index is exclusive.

Here is an example of slicing a list to retrieve the first three elements:

my_list = [1, 2, 3, 4, 5]
first_three_elements = my_list[0:3]
print(first_three_elements)

Output:

[1, 2, 3]

 

You can also use negative indexing to access elements from the end of the list. For example, to access the last element in the list, you can use -1 as the index:

my_list = [1, 2, 3, 4, 5]
last_element = my_list[-1]
print(last_element)

Output:

5 

Adding and Removing Elements from Lists

Lists are mutable, which means that you can add or remove elements from them after they have been created.

To add an element to the end of a list, you can use the append() method:

my_list = [1, 2, 3, 4, 5]
my_list.append(6)
print(my_list)

Output:

[1, 2, 3, 4, 5, 6]

 

To insert an element at a specific position in a list, you can use the insert() method:

my_list = [1, 2, 3, 4, 5]
my_list.insert(3, 3.5)
print(my_list)

Output:

[1, 2, 3, 3.5, 4, 5] 

 

To remove an element from a list, you can use the remove() method:

my_list = [1, 2, 3, 4, 5]
my_list.remove(3)
print(my_list)

Output:

[1, 2, 4, 5]

 

Sorting Lists

You can sort a list in Python using the sort() method. By default, the sort() method sorts the list in ascending order.

Here is an example of sorting a list of numbers:

my_numbers = [4, 1, 3, 2, 5]
my_numbers.sort()
print(my_numbers)

Output:

[1, 2, 3, 4, 5]

 

You can also sort a list in descending order by passing the argument reverse=True to the sort() method:

my_numbers = [4, 1, 3, 2, 5]
my_numbers.sort(reverse=True)
print(my_numbers)

Output:

[5, 4, 3, 2, 1]

 

 

Looping Through Lists

Looping through a list is a common operation in Python. You can use `for` loops to iterate over the elements of a list. Here is an example of looping through a list of strings:

fruits = ["apple", "banana", "cherry"]
for fruit in fruits:
    print(fruit)

Output:

apple
banana
cherry

 

You can also loop through a list using its indices with the range() function:

fruits = ["apple", "banana", "cherry"]
for i in range(len(fruits)):
    print(fruits[i])

Output:

apple banana cherry 

 

Exercises

Now that you have learned the basics of lists in Python, it's time to practice! Here are some exercises to help you solidify your understanding:

  1. Create a list of your favorite colors and print its second element.
  2. Create a list of numbers from 1 to 10 and slice it to get the odd numbers.
  3. Add the number 7 to the list of numbers from 1 to 10 and then sort the list in descending order.
  4. Loop through the list of your favorite colors and print each color in uppercase.

Conclusion

Lists are an essential part of programming in Python. They allow you to store collections of data and manipulate them in various ways. In this lesson, you have learned how to create lists, access their elements using indexing and slicing, add and remove elements from them, sort them, and iterate through them using loops. Keep practicing these concepts, and you will be well on your way to mastering Python lists!


user

Robort Gabriel

Lagos, Nigeria

Freelance Web Developer, Native Android Developer, and Coding Tutor.

Skills
  • UI / UX
  • JavaScript
  • Python
  • PHP
  • Kotlin
  • Java
  • Bootrap
  • Android
  • Laravel