0% found this document useful (0 votes)
3 views4 pages

pyt3

The document provides an overview of lists in Python, detailing how to define them, access elements, and manipulate their contents using methods like append and remove. It also explains nested lists, list indexing, and slicing techniques. Examples illustrate the various functionalities and applications of lists, including their use in representing structures like a 2D game board.

Uploaded by

sandhyadevit
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views4 pages

pyt3

The document provides an overview of lists in Python, detailing how to define them, access elements, and manipulate their contents using methods like append and remove. It also explains nested lists, list indexing, and slicing techniques. Examples illustrate the various functionalities and applications of lists, including their use in representing structures like a 2D game board.

Uploaded by

sandhyadevit
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Lists in Python

Now that we've covered the basic data types in Python, let's start covering the built-in data
structures. First, we have lists.

To define a list, we use square brackets [] with the elements separated by a comma.

💡 Tip: It's recommended to add a space after each comma to make the code more readable.

For example, here we have examples of lists:

[1, 2, 3, 4, 5]
["a", "b", "c", "d"]
[3.4, 2.4, 2.6, 3.5]

Lists can contain values of different data types, so this would be a valid list in Python:

[1, "Emily", 3.4]

We can also assign a list to a variable:

my_list = [1, 2, 3, 4, 5]
letters = ["a", "b", "c", "d"]

Nested Lists

Lists can contain values of any data type, even other lists. These inner lists are called nested
lists.

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

In this example, [1, 2, 3] and [4, 5, 6] are nested lists.

Here we have other valid examples:

[["a", "b", "c"], ["d", "e", "f"], ["g", "h", "i"]]


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

We can access the nested lists using their corresponding index:

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

>>> my_list[0]
[1, 2, 3]

>>> my_list[1]
[4, 5, 6]

Nested lists could be used to represent, for example, the structure of a simple 2D game board
where each number could represent a different element or tile:

# Sample Board where:


# 0 = Empty tile
# 1 = Coin
# 2 = Enemy
# 3 = Goal
board = [[0, 0, 1],
[0, 2, 0],
[1, 0, 3]]

List Length

We can use the len() function to get the length of a list (the number of elements it contains).

For example:

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

>>> len(my_list)
4

Update a Value in a List

We can update the value at a particular index with this syntax:

<list_variable>[<index>] = <value>

For example:

>>> letters = ["a", "b", "c", "d"]

>>> letters[0] = "z"

>>> letters
['z', 'b', 'c', 'd']

Add a Value to a List

We can add a new value to the end of a list with the .append() method.

For example:

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

>>> my_list.append(5)

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

Remove a Value from a List

We can remove a value from a list with the .remove() method.

For example:

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

>>> my_list.remove(3)
>>> my_list
[1, 2, 4]

💡 Tip: This will only remove the first occurrence of the element. For example, if we try to
remove the number 3 from a list that has two number 3s, the second number will not be
removed:

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

>>> my_list.remove(3)

>>> my_list
[1, 2, 3, 4]

List Indexing

We can index a list just like we index strings, with indices that start from 0:

>>> letters = ["a", "b", "c", "d"]

>>> letters[0]
'a'

>>> letters[1]
'b'

>>> letters[2]
'c'

>>> letters[3]
'd'

List Slicing

We can also get a slice of a list using the same syntax that we used with strings and we can
omit the parameters to use their default values. Now, instead of adding characters to the slice,
we will be adding the elements of the list.

<list_variable>[start:stop:step]

For example:

>>> my_list = ["a", "b", "c", "d", "e", "f", "g", "h", "i"]

>>> my_list[2:6:2]
['c', 'e']

>>> my_list[2:8]
['c', 'd', 'e', 'f', 'g', 'h']

>>> my_list[1:10]
['b', 'c', 'd', 'e', 'f', 'g', 'h', 'i']

>>> my_list[4:8:2]
['e', 'g']

>>> my_list[::-1]
['i', 'h', 'g', 'f', 'e', 'd', 'c', 'b', 'a']

>>> my_list[::-2]
['i', 'g', 'e', 'c', 'a']

>>> my_list[8:1:-1]
['i', 'h', 'g', 'f', 'e', 'd', 'c']

You might also like