Many Things You Can Do With Python Lists You May Have Missed
Tags: python, tutorial, beginners, codenewbie
Published: 2020-02-05 01:06:48

**Subscribe to my email list now at http://jauyeung.net/subscribe/**

**Follow me on Twitter at https://twitter.com/AuMayeung**

**Many more articles at https://medium.com/@hohanga**

**Even more articles at http://thewebdev.info/**

Python is a convenient language that’s often used for scripting, data science, and web development.

In this article, we’ll look at the many things that we can do with Python lists.

### Sorting the Values in a List with the sort() Method

We can use the list `sort` method to sort a list. It works with any object, including strings and numbers.

For example, we can use it as follows:

```python
fruits = ['apple', 'orange', 'orange', 'grape', 'pear']  
fruits.sort()
```

The sorting is done in place. So it sorts the list that it’s called on.

Therefore, we get:

```python
['apple', 'grape', 'orange', 'orange', 'pear']
```

as the new value of `fruits`.

With numbers, we can write:

```python
nums = [5,3,4,6,7]  
nums.sort()
```

Then the new value of `nums` is:

```python
[3, 4, 5, 6, 7]
```

We can’t sort lists that have mixed data types.

For example, the following list:

```python
mixed = ['apple', 'orange',3,2,1]  
mixed.sort()
```

We’ll get:

TypeError: '<' not supported between instances of 'int' and 'str'

since `sort` can’t compare the entries.

### Reversing the Values in a List with the reverse() Method

The `reverse` method reverses a list in place. It can also be done with string and number arrays.

For instance, we can write:

```python
fruits = ['apple', 'orange', 'orange', 'grape', 'pear']  
fruits.reverse()
```

Then the new value of `fruits` is:

```python
['pear', 'grape', 'orange', 'orange', 'apple']
```

Likewise, we can do the same with a number list:

```python
nums = [5,3,4,6,7]  
nums.reverse()
```

Then we get:

```python
[7, 6, 4, 3, 5]
```

as the new value of `nums`.

### Emptying a List with clear()

We can call `clear` on a list to clear all entries from a list.

For instance, we can call it as follows:

```python
nums = [5,3,4,6,7]  
nums.clear()
```

Then we get that the new value of `nums` is an empty list.

### Remove the Last Element of a List with pop()

We can call `pop` on a list to remove the last item off a list.

For instance, we can write the following:

```python
nums = [5,3,4,6,7]  
nums.pop()
```

to remove 7 from `nums`.

### Counting the Number of Times an Item Appears on a List with count()

The `count` method takes an element that we want to search for in a list.

For instance, we can use it as follows:

```python
fruits = ['apple', 'orange', 'orange', 'grape', 'pear']  
orange_count = fruits.count('orange')
```

`orange_count` should be 2 since `'orange'` appeared twice on the list.

### Shallow Copying a List with copy()

The `copy` method creates a shallow copy of the array it’s called on and returns it. This is the same as `a[:]`.

A shallow copy means only the top-level items are copied. Nested items are still referencing the original items.

For instance, given that we have the following code:

```python
fruits = ['apple', 'orange', 'orange', 'grape', 'pear']  
fruits_copy = fruits.copy()  
fruits_copy.append('banana')
```

We should see that the value of `fruits` is:

```python
['apple', 'orange', 'orange', 'grape', 'pear']
```

and the value of `fruits_copy` is:

```python
['apple', 'orange', 'orange', 'grape', 'pear', 'banana']
```

This is because we made a copy of the `fruits` list by calling `copy`. Therefore, `fruits_copy` is referencing a new list with the same entries as `fruits` in the second line.

### List Comprehensions

We can use the list comprehension syntax to create a list by mapping list items by calling a function to do the mapping.

For instance, we can write:

```python
cubes = [x**3 for x in range(10)]
```

to create an array with each entry a cube of the entry returned from `range(10)` .

Therefore, we should get:

```python
[0, 1, 8, 27, 64, 125, 216, 343, 512, 729]
```

as the value of `cubes` since `0**3` is 0, `1**3` is 1, `2**3` is 8, and so on.

We can also use it to returns a new list with some items filtered out as follows:

```python
nums = [-3,5,6]  
positive_nums = [x for x in nums if x > 0]
```

The code above takes all the numbers that are bigger than 0 on the list and returns it.

Therefore, we should get:

```python
[5, 6]
```

as the value of `positive_nums`.

### Conclusion

Python lists have lots of methods to do various operations.

We can sort lists with `sort`, remove the last item from a list with `pop`, count the number of items we’re looking for with `count`, and empty a list with `clear`.

Python also has the list comprehension syntax to generate a list from another list.