DEV Community is a community of 867,901 amazing developers

We're a place where coders share, stay up-to-date and grow their careers.

CodeWithKenn

Posted on

Python Lists Cheat Sheet - Developer Resources

Hey, there!

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.

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 - 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 - 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 - 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 - 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 - 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 - 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 - 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

All the Credit goes to @ShyamaSankar