# Lists

Python Lists are ordered collections of items, where each item can be of any data type, and they are enclosed within square brackets `[]`. Lists support various operations such as appending, indexing, slicing, and iterating. They can contain heterogeneous data types, allowing flexibility in storing different kinds of elements like integers, strings, floats, or even other lists (nested lists).  In addition, they are mutable, meaning their elements can be changed after creation, making them versatile for dynamic data storage and manipulation. Furthermore, they  support indexing and slicing to access elements or sublists; and have their own methods, which we will see below.

Dataset:

```python
basketball_champions= [
    'Los Angeles Lakers', 17,
    'Boston Celtics', 17,
    'Golden State Warriors', 7,
    'Chicago Bulls', 6,
    'San Antonio Spurs', 5,
    'Philadelphia 76ers', 3,
    'Detroit Pistons', 3,
    'Miami Heat', 3,
    'New York Knicks', 2,
    'Houston Rockets', 2,
    'Milwaukee Bucks', 2,
    'Cleveland Cavaliers', 1,
    'Atlanta Hawks', 1,
    'Washington Wizards', 1,
    'Oklahoma City Thunder', 1,
    'Portland Trail Blazers', 1,
    'Dallas Mavericks',	1,
    'Sacramento Kings',	1,
    'Toronto Raptors', 1,
    'Denver Nuggets', 1
]
```

## 1. Subsetting Lists

### 1.1. Single Element&#x20;

```python
# number of elements/items in a list
num = len(basketball_champions)
print(f"Number of elements: {num}")  # 40

# last element
try: 
    print(basketball_champions[40])  # error, indexing start at 0
except IndexError:
    print("Oops! Out of Index!")

# last element (again)
print(basketball_champions[39])
#or 
print(basketball_champions[num-1])
#or (preferrable)
print(basketball_champions[-1])

# first element
print(basketball_champions[0])


# OUTPUT
"""
Number of elements: 40
Oops! Out of Index!
1
1
1
Los Angeles Lakers
"""
```

### 1.2. Multiple Elements (Slicing)

```python
# print first two teams with their championships
print(basketball_champions[0:4])

# print last two teams with their championships
print(basketball_champions[-4:])

# print only teams
print(basketball_champions[::2])


# OUTPUT
"""
['Los Angeles Lakers', 17, 'Boston Celtics', 17]
['Toronto Raptors', 1, 'Denver Nuggets', 1]
['Los Angeles Lakers', 'Boston Celtics', 'Golden State Warriors', 'Chicago Bulls', 'San Antonio Spurs']
"""
```

## 2. Updating Lists

### 2.1. Changing list elements

```python
# change championship for Lakers to 25
basketball_champions[1] = 25
print(basketball_champions[:2])

# increase championship for Denver Nuggets
basketball_champions[-1] = 2
print(basketball_champions[-2:])


# OUTPUT
"""
['Los Angeles Lakers', 25]
['Denver Nuggets', 2]
"""
```

### 2.2. Adding and removing elements

```python
# adding Phoenix Suns with 0 championships to our list
basketball_champions += ['Phoenix Suns',0]
print(basketball_champions[-6:])


# 1.Deleting by Index
# a. using del
# deleting 'Phoenix Suns' and its championships value from the list
del basketball_champions[-2:]
print(basketball_champions[-6:])

# b. using .pop()
# deleting the last element will be deleted 
basketball_champions.pop() # if no index given, then the last element will be deleted
# or the same as basketball_champions.pop(-1)
print(basketball_champions[-6:])


# Deleting by Value
# removing a specific item (first occurence of that item!)
basketball_champions.remove('Denver Nuggets')
print(basketball_champions[-6:])

# OUTPUT
"""
['Toronto Raptors', 1, 'Denver Nuggets', 2, 'Phoenix Suns', 0]
['Sacramento Kings', 1, 'Toronto Raptors', 1, 'Denver Nuggets', 2]
[1, 'Sacramento Kings', 1, 'Toronto Raptors', 1, 'Denver Nuggets']
['Dallas Mavericks', 1, 'Sacramento Kings', 1, 'Toronto Raptors', 1]
"""
```

{% hint style="info" %}
In python **del** is a keyword and **remove(), pop()** are in-built methods (see section #3 for more methods). The difference between **del** and **pop()** is that even though they both delete the values at a particular index, **pop()** returns the deleted value:

```python
del basketball_champions[0]
# No OUTPUT

basketball_champions.pop(0)
# OUTPUT: 'Los Angeles Lakers'
```

{% endhint %}

## 3. List Methods

<table><thead><tr><th width="136">Method</th><th>Description</th></tr></thead><tbody><tr><td><a href="https://www.w3schools.com/python/ref_list_append.asp">append()</a></td><td>Adds an element at the end of the list</td></tr><tr><td><a href="https://www.w3schools.com/python/ref_list_clear.asp">clear()</a></td><td>Removes all the elements from the list</td></tr><tr><td><a href="https://www.w3schools.com/python/ref_list_copy.asp">copy()</a></td><td>Returns a copy of the list</td></tr><tr><td><a href="https://www.w3schools.com/python/ref_list_count.asp">count()</a></td><td>Returns the number of elements with the specified value</td></tr><tr><td><a href="https://www.w3schools.com/python/ref_list_extend.asp">extend()</a></td><td>Add the elements of a list (or any iterable), to the end of the current list</td></tr><tr><td><a href="https://www.w3schools.com/python/ref_list_index.asp">index()</a></td><td>Returns the index of the first element with the specified value</td></tr><tr><td><a href="https://www.w3schools.com/python/ref_list_insert.asp">insert()</a></td><td>Adds an element at the specified position</td></tr><tr><td><a href="https://www.w3schools.com/python/ref_list_pop.asp">pop()</a></td><td>Removes the element at the specified position</td></tr><tr><td><a href="https://www.w3schools.com/python/ref_list_remove.asp">remove()</a></td><td>Removes the first item with the specified value</td></tr><tr><td><a href="https://www.w3schools.com/python/ref_list_reverse.asp">reverse()</a></td><td>Reverses the order of the list</td></tr><tr><td><a href="https://www.w3schools.com/python/ref_list_sort.asp">sort()</a></td><td>Sorts the list</td></tr></tbody></table>

&#x20;                                        Click on the methods practice them on W3schools

```python
# insert Chicago Bulls and 99 to the list
basketball_champions.insert(0,'Chicago Bulls')
basketball_champions.insert(1,99)
print(basketball_champions[:6])

# retrieve Chicago Bulls index
print(basketball_champions.index('Chicago Bulls'))

# retrieve Chicago Bulls' original index
print(basketball_champions.index('Chicago Bulls',1))

# count the frequency of a specific value
print(f'Number of "Chicago Bulls" entries: {basketball_champions.count("Chicago Bulls")}')
print(f"Number of teams with single championships: {basketball_champions.count(1)}")

# OUTPUT
"""
['Chicago Bulls', 99, 'Los Angeles Lakers', 17, 'Boston Celtics', 17]
0
8
Number of "Chicago Bulls" entries: 2
Number of teams with single championships: 9
"""

# delete Chicago Bulls with 99 championships
basketball_champions.pop(0)
basketball_champions.pop(0)
```

Lists are great data types to store data; however, they fall short when it comes to arithmetic:

```python
# extract championships only to do arithmetic
champs = basketball_champions[1::2]
print(champs)

# multiply each element with itself
champs * champs

# OUTPUT
"""
[17, 17, 7, 6, 5, 3, 3, 3, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1]
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[76], line 4
      1 champs = basketball_champions[1::2]
      2 print(champs)
----> 4 champs * champs

TypeError: can't multiply sequence by non-int of type 'list'
"""
```

To do such calculation we need the help of a `for` loop:

```python
# elementwise multiplication
ch_ch = [i*i for i in champs]
print(ch_ch)

# OUTPUT
"""
[289, 289, 49, 36, 25, 9, 9, 9, 4, 4, 4, 1, 1, 1, 1, 1, 1, 1, 1, 1]
"""
```

A better approach would be to use the [Numpy](https://numpy.org/) package! We first convert our list to a numpy array and then can do the multiplication without having use of a `for` loop.

```python
import numpy as np

arr_champs = np.array(champs)
print(arr_champs * arr_champs)

# OUTPUT
"""
[289 289  49  36  25   9   9   9   4   4   4   1   1   1   1   1   1   1   1   1]
"""
```

## Conclusion

Python lists are versatile data structures that can hold a collection of elements of different types. They are mutable, meaning we can modify their contents after creation. Lists support indexing and slicing to access elements or sublists and have built-in methods to operate on them more easily. They are dynamic in size and can grow or shrink as needed, making them a fundamental building block for many Python programs.&#x20;

All being said, because of the nature of the dataset we used (i.e. having only pairs of  'team' and 'championships') it is better if we use another Python's data structure called [Dictionary](https://dshub.gitbook.io/ds-hub/python/basics/variables-and-datatypes/dictionaries).&#x20;


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://dshub.gitbook.io/ds-hub/python/basics/variables-and-datatypes/lists.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
