Problem Formulation and Solution Overview
Method 1: Use Slicing
This issue can be solved using Python’s Slicing Slicing
using two (2) lines of code.
populations = [336997624, 59597300, 38246108, 338289857, 68507026, 38411751] pop_in_2021 = populations[:len(populations)//2] pop_in_2022 = populations[len(populations)//2:] print(pop_in_2021, pop_in_2022)
Above declares a list of six (6) populations. The first half represents the population of the US, UK and Canada for 2021. In the second half, the population of the US, UK and Canada for 2022. This list
saves to populations
.
To extract the population data for 2021, populations
is sliced from the start position of zero (0) to halfway through the list (or three elements in) using popluation // 2
. This slicing equates to the following:
populations[0:3] |
To extract the population data for 2022, populations
is sliced from the start position of three (3), which equates to populations // 2
.
The end position is the remaining length of populations
. This slicing equates to the following:
populations[3:] |
π‘Note: Floor Division rounds down to the nearest integer value. In this case, three (3).
The output is sent to the terminal. The 2021 list
is displayed on the left, and the 2022 list
is on the right.
[336997624, 59597300, 38246108] [338289857, 68507026, 38411751] |
Method 2: Use List Comprehension
This method uses List Comprehension
, slicing
and range()
to split a single list into a list with two (2) nested lists
using one (1) line of code!
populations = [336997624, 59597300, 38246108, 338289857, 68507026, 38411751] all_pops = [populations[x:x+3] for x in range(0,len(populations),3)] print(all_pops)
Above declares a list of six (6) populations. The first half represents the population of the US, UK and Canada for 2021. In the second half, the population of the US, UK and Canada for 2022. This list
saves to populations
.
To split the list into two (2) nested lists, List Comprehension
is used in conjunction with slicing
and range()
to loop through and determine where to split. In this case, every three (3) elements.
The output is sent to the terminal and displays the 2021 data as the first nested list and the 2022 data as the second nested list.
[[336997624, 59597300, 38246108], [338289857, 68507026, 38411751]] |
πA Finxter Favorite!
Method 3: Use a Function and Slicing
This method creates a function that accepts a list and uses the Right Shift Operator to split it into a Tuple
, each containing its own list.
def split_half(pop): half = len(pop) >> 1 return pop[:half], pop[half:] populations = [336997624, 59597300, 38246108, 338289857, 68507026, 38411751] print(split_half(populations))
Above defines a function with one (1) argument (split_half(pop)
).
This function splits the list
in half using the Right Shift Operator
and returns the results as a Tuple
with two (2) nested lists.
([336997624, 59597300, 38246108], [338289857, 68507026, 38411751]) |
π‘Note: The Right Shift Operator
works the same as Floor Division.
Method 4: Use islice()
This method imports the itertools
library to use the islice()
function: an efficient way to iterate through a list.
from itertools import islice populations = [336997624, 59597300, 38246108, 338289857, 68507026, 38411751] len_split = [len(populations)//2]*2 results = [list(islice(iter(populations), elem)) for elem in len_split] print(results)
Above imports the itertools
library to call in and use the islice()
function.
The following line declares a list of six (6) populations. The first half represents the population of the US, UK and Canada for 2021. In the second half, the population of the US, UK and Canada for 2022. This list
saves to populations
.
Next, where to split populations
is determined based on the calculation ([len(popluations)//2]*2
). This saves to len_split
and equates to the following:
[3, 3] |
Finally, List Comprehension
is used along with islice()
to iterate through populations
and split the list into two (2) based on len_split
([3, 3]).
The output is sent to the terminal and displays the 2021 data as the first nested list
and the 2022 data as the second nested list
.
[[336997624, 59597300, 38246108], [338289857, 68507026, 38411751]] |
π‘Note: The islice()
function allows the user to loop through an iterable with a start and stop and return, in this case, an object which is then converted to a list
.
Method 5: Use accumulate()
This method imports the itertools
library to use the accumulate()
function.
from itertools import accumulate populations = [336997624, 59597300, 38246108, 338289857, 68507026, 38411751] len_split = [len(populations)//2]*2 results = [populations[x - y: x] for x, y in zip(accumulate(len_split), len_split)] print(results)
Above imports the itertools
library to call in and use the accumulate()
function.
The following line declares a list of six (6) populations. The first half represents the population of the US, UK and Canada for 2021. In the second half, the population of the US, UK and Canada for 2022. This list
saves to populations
.
Next, where to split populations
is determined based on the calculation ([len(popluations)//2]*2
). This saves to len_split
and equates to the following:
[3, 3] |
Finally, List Comprehension
is used along with zip()
and accumulate()
to iterate through populations
and split the list
into two (2) based on len_split
([3, 3]).
The output is sent to the terminal and displays the 2021 data as the first nested list
and the 2022 data as the second nested list
.
[[336997624, 59597300, 38246108], [338289857, 68507026, 38411751]] |
Summary
Programmer Humor
π±ββοΈ Programmer 1: We have a problem
π§ββοΈ Programmer 2: Letβs use RegEx!
π±ββοΈ Programmer 1: Now we have two problems
… yet – you can easily reduce the two problems to zero as you polish your “RegEx Superpower in Python“. π