CodeGym /Courses /Python SELF EN /List Collection

List Collection

Python SELF EN
Level 7 , Lesson 1
Available

1.1 List of Elements

In Python, there are several built-in data types that let you store a group of objects. These are list, tuple, set, dictionary. Today, we're gonna check out the simplest and most used one — the list.

A List allows you to store a list of elements. Each element comes with an assigned order number. That's why our list is called ordered. As we'll see later, not all collections in Python are like this.

Also, you can modify your list by adding new elements, removing old ones, changing the order of the elements, and so on. These actions give us the second characteristic of our list — changeable. Not every collection has this property either.

The third important characteristic of a list is that it lets you store the same elements multiple times — allow duplicates. We'll dive deeper into all these characteristics below.

1.2 Creating a List

You can create a list in several ways.

Using Square Brackets

Lists in Python are created using square brackets [], with elements separated by commas. For example:


my_list = [1, 2, 3, 'apple', 'banana']
        

List elements can be of different types, including numbers, strings, and even other lists, which makes lists extremely versatile.

To create an empty list, you can just use empty square brackets:


empty_list = []
        

An empty list can be useful if you want to create a structure for dynamically adding elements while the program is running. For example, you might start with an empty list and add elements based on conditions, user input, or computation results.

Using the list() Function

The list() function turns the given element into a list.


my_list = list('hello')
        

This will be a list with a single element.

If you need to convert multiple elements into a list, you should pass them to the list function as a (tuple). To do this, wrap your elements in parentheses. Example:


my_list = list((1, 2, 3, 'apple', 'banana'))
        

You can also create an empty list using the list() function by not passing any arguments:


empty_list = list()
        

1.3 List Methods

Lists in Python come with a bunch of methods that let you efficiently manage collections of data. Here are some of the most popular methods of the list class:

Method Description
append() Adds an element to the end of the list.
extend() Extends the list by appending all the elements from the specified sequence.
insert() Inserts an element at the specified position.
remove() Removes the first occurrence of the element.
pop() Removes and returns the element at the given index.
clear() Removes all items from the list.
index() Returns the index of the first occurrence of the element.
count() Counts how many times an element appears in the list.
sort() Sorts the elements of the list in place.
reverse() Reverses the elements of the list in place.

We'll go over the details of how these methods work and even a little more down the line.

2
Task
Python SELF EN, level 7, lesson 1
Locked
Write a program that creates a list of squares for numbers from -100 to 100. Use the list() function to create the list.
Write a program that creates a list of squares for numbers from -100 to 100. Use the list() function to create the list.
2
Task
Python SELF EN, level 7, lesson 1
Locked
Write a program that creates an empty list using square brackets [] and adds elements to it, requested from the user.
Write a program that creates an empty list using square brackets [] and adds elements to it, requested from the user.
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION