DEV Community

Cover image for Python Lists Cheat Sheet - Developer Resources
CodeWithKenn
CodeWithKenn

Posted on

Python Lists Cheat Sheet - Developer Resources

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

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

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

Enter fullscreen mode Exit fullscreen mode

Step 2

Snippet - Image

carbon (2).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?"

Enter fullscreen mode Exit fullscreen mode

Step 3

Snippet - Image

carbon (3).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?"]

Enter fullscreen mode Exit fullscreen mode

Step 4

Snippet - Image

carbon (4).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"]


Enter fullscreen mode Exit fullscreen mode

Step 5

Snippet - Image

carbon (5).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?"]

Enter fullscreen mode Exit fullscreen mode

Step 6

Snippet - Image

carbon-lists-2.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])


Enter fullscreen mode Exit fullscreen mode

Step 7

Snippet - Image

carbon-lists-2-1.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]]

Enter fullscreen mode Exit fullscreen mode

Credit - Cheat Sheet

carbon.png

All the Credit goes to @ShyamaSankar

🌎 Before Leaving

Want to start blogging? 🔥Join NOW!

Top comments (0)