Python Lists Cheat Sheet - Developer Resources
Tags: python, programming, tutorial, beginners
Published: 2022-02-14 17:43:00

Hey, there!


This is Kenn, Your Daily Advocate from **CodeWithKenn**!
Welcome to the Blog! Make yourself at home! 

In this article, we won't talk much. For a couple of days, I've been thinking about a way of making my content easier and shorter so that Developers won't have to spend so much time scrolling; I want this Blog a Place of Resources and Discovery, not a Boring Blog.


![save-your-time-2a7fb4.jpg](https://cdn.hashnode.com/res/hashnode/image/upload/v1644857915236/HNJherNb5.jpeg)


# What are Python Lists?


Today, we're gonna get one of the first resources, Python Lists Easy Cheatsheet.

Python Lists are used to store an ordered collection of items, which might be of different types but usually they aren't.

# Cheatsheet Content

## Step 1


### Snippet - Image

![carbon (1).png](https://cdn.hashnode.com/res/hashnode/image/upload/v1644858459662/CGjzyuSpc.png)

### Snippet - Code


```
# Create an empty list using square brackets.
numbers = []
print(numbers) # Output: []

# Create an empty list using list().
numbers = list()
print(numbers) # Output: []

# Create a list of numbers.
numbers = [1, 2, 3]
print(numbers) # Output: [1, 2, 3]

# Create a list of numbers in a range.
numbers = list(range(1, 4))
print(numbers) # Output: [1, 2, 3]

# Create a list of tuples.
tuples_list = [(1, 2), (2, 4), (3, 6)]
print(tuples_list) # Output: [(1, 2), (2, 4), (3, 6)]

``` 


## Step 2


### Snippet - Image


![carbon (2).png](https://cdn.hashnode.com/res/hashnode/image/upload/v1644858667007/4gPld_KaY.png)


### Snippet - Code


```
# Create a list of lists.
list_of_lists = [[1, 2], [2, 4], [3, 6]]
print(list_of_lists) # Output: [[1, 2], [2, 4], [3, 6]]

# Create a list with items of different data types.
random_list = [1, "hey", [1, 2]]
print(random_list) # Output: [1, "hey", [1, 2]]

# Get length of list by using len() method.
numbers = [5, 8, 8]
print(len(numbers)) # Output: 3

# Access elements of a list by indexing.
str_list = ["hey", "there!", "how", "are", "you?"]
print(str_list[0]) # Output: "hey"
print(str_list[len(str_list) - 1]) # Output: "you?"
print(str_list[-1]) # Output: "you?"

``` 


## Step 3


### Snippet - Image



![carbon (3).png](https://cdn.hashnode.com/res/hashnode/image/upload/v1644858847484/c0ND1W-Sd.png)



### Snippet - Code



```
# Slicing a list.
str_list = ["hey", "there!", "how", "are", "you?"]
print(str_list[2:]) # Output: ["how", "are", "you?"]
print(str_list[:2]) # Output: ["hey", "there!"]
print(str_list[-3:]) # Output: ["how", "are", "you?"]
print(str_list[:-3]) # Output: ["hey", "there!"]
print(str_list[1:4]) # Output: ["there!", "how", "are"]
# Get a copy of list by slicing.
print(str_list[:]) # Output: ["hey", "there!", "how", "are", "you?"]

``` 


## Step 4


### Snippet - Image




![carbon (4).png](https://cdn.hashnode.com/res/hashnode/image/upload/v1644859067103/nPyMnPAEf.png)



### Snippet - Code



```

# Append to a list.
numbers = [1, 2]
print(numbers) # Output: [1, 2]
numbers.append(3)
print(numbers) # Output: [1, 2, 3]

# Concatenate lists.
numbers = [1, 2]
strings = ["Hey", "there"]
print(numbers + strings) # Output: [1, 2, "Hey", "there"]


``` 

## Step 5


### Snippet - Image



![carbon (5).png](https://cdn.hashnode.com/res/hashnode/image/upload/v1644859171328/c4_s-MMOg.png)



### Snippet - Code


```
# Mutate a list, that is, change its contents.
numbers = [1, 2, 3]
numbers[0] = 100
print(numbers) # Output: [100, 2, 3]
numbers[0:2] = [300, 400]
print(numbers) # Output: [300, 400, 3]
numbers[1:3] = []
print(numbers) # Output: [300]
numbers[:] = []
print(numbers) # Output: []

# Insert item to a list.
greeting = ["how", "you?"]
greeting.insert(1, "are")
print(greeting) # Output: ["how", "are", "you?"]

``` 


## Step 6


### Snippet - Image




![carbon-lists-2.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1644859543800/asWaM8DPR.png)



### Snippet - Code


```
# Our list of lists.
matrix_1 = [[1,1,1], [2,2,2], [3,3,3]]
matrix_2 = [[4,4,4], [5,5,5], [6,6,6]]

# Matrix addition with for loop.
# Assuming that the matrices are of the same dimensions
matrix_sum = []
for row in range(len(matrix_1)):
  matrix_sum.append([])
  for column in range(len(matrix[0])):
      matrix_sum[row].append(matrix_1[row][column] + matrix_2[row][column])
  

``` 


## Step 7


### Snippet - Image



![carbon-lists-2-1.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1644859582127/JjYy8T4TYT.png)



### Snippet - Code


```

# Our list of lists.
matrix_1 = [[1,1,1], [2,2,2], [3,3,3]]
matrix_2 = [[4,4,4], [5,5,5], [6,6,6]]


# Rewrite using list comprehension only for inner lists.
matrix_sum = []
for row in range(len(matrix_1)):
  matrix_sum.append([matrix_1[row][column] + matrix_2[row][column] for column in range(len(matrix[0]))])

# Rewrite using nested list comprehension.
matrix_sum = [[matrix_1[row][column] + matrix_2[row][column] for column in range(len(matrix[0]))]
              for row in range(len(matrix_1))]

print(matrix_sum) # Output: [[5, 5, 5], [7, 7, 7], [9, 9, 9]]

``` 


## Credit - Cheat Sheet



![carbon.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1644859646012/41yYXVXuE.png)


⭐ **All the Credit goes to [@ShyamaSankar](https://gist.github.com/ShyamaSankar)**









# 🌎 Before Leaving


- ⚡ [Discover Amazing Python Resources FOR FREE!](https://codewithkenn.hashnode.dev/discover-amazing-python-resources-for-free)

- ⚡ [YOU NEED to Learn Python in 2022! (Business Perspective)](https://codewithkenn.hashnode.dev/you-need-to-learn-python-in-2022-business-perspective)


- [Twitter](https://twitter.com/CodeWithKenn) 
- [Github](https://github.com/KennStack01) 
- [LinkedIn](https://www.linkedin.com/in/kennkibadi/) 
- [Instagram](https://www.instagram.com/codewithkenn_/) 
- [Facebook](https://www.facebook.com/Code-With-Kenn-104376625415201)
- [See My Bio Link](https://www.bio.link/IamKenn)



Want to start blogging?  🔥[Join NOW!](https://hashnode.com/@CodeWithKenn/joinme)