Easy Lists
py_simple.easy_lists
Beginner-friendly helpers for common list operations.
alternate_lists(list_a, list_b)
Combines two lists by taking turns, one item at a time.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
list_a
|
list
|
First list. |
required |
list_b
|
list
|
Second list. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
list |
list
|
Alternating list, remaining items appended at the end. |
Example
from py_simple import alternate_lists
result = alternate_lists([1, 2], [3, 4]) # -> [1, 3, 2, 4]
list_a, list_b = [1, 2], [3, 4]
result = [item for pair in zip(list_a, list_b) for item in pair]
chunk_list(items, size)
Splits a list into smaller lists of the given size.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
items
|
list
|
List to split. |
required |
size
|
int
|
Maximum size of each chunk. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
list |
list
|
List of chunks. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If size is less than 1. |
Example
from py_simple import chunk_list
result = chunk_list([1, 2, 3, 4, 5], 2) # -> [[1, 2], [3, 4], [5]]
items, size = [1, 2, 3, 4, 5], 2
result = [items[i:i + size] for i in range(0, len(items), size)]
find_duplicates(items)
Returns a list of items that appear more than once.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
items
|
list
|
List to check for duplicates. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
list |
list
|
Duplicated items, in the order they first appear. |
Example
from py_simple import find_duplicates
result = find_duplicates([1, 2, 2, 3, 3, 3]) # -> [2, 3]
items = [1, 2, 2, 3, 3, 3]
result = list({item for item in items if items.count(item) > 1})
flatten_list(items)
Flattens a list with nested lists into a single-level list.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
items
|
list
|
List that may contain nested lists. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
list |
list
|
Flattened list, one level deep. |
Example
from py_simple import flatten_list
result = flatten_list([[1, 2], [3]]) # -> [1, 2, 3]
items = [[1, 2], [3]]
result = [item for sublist in items for item in sublist]
merge_lists(list_a, list_b)
Combines two lists into one.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
list_a
|
list
|
First list. |
required |
list_b
|
list
|
Second list. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
list |
list
|
Combined list, list_b items after list_a items. |
Example
from py_simple import merge_lists
result = merge_lists([1, 2], [3, 4]) # -> [1, 2, 3, 4]
list_a, list_b = [1, 2], [3, 4]
result = list_a + list_b
most_common_item(items)
Returns the item that appears most often in a list.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
items
|
list
|
List to examine. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
object |
object
|
The most frequent item. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the list is empty. |
Example
from py_simple import most_common_item
result = most_common_item([1, 1, 2]) # -> 1
from collections import Counter
items = [1, 1, 2]
result = Counter(items).most_common(1)[0][0]
rotate_list(items, steps)
Rotates a list to the right by the given number of steps.
Negative steps rotate to the left.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
items
|
list
|
List to rotate. |
required |
steps
|
int
|
Number of positions to rotate. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
list |
list
|
Rotated list. |
Example
from py_simple import rotate_list
result = rotate_list([1, 2, 3], 1) # -> [3, 1, 2]
items, steps = [1, 2, 3], 1
steps = steps % len(items)
result = items[-steps:] + items[:-steps]
sort_numbers(items)
Returns a new list of numbers sorted from smallest to largest.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
items
|
list
|
List of numbers. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
list |
list
|
Sorted list of numbers. |
Example
from py_simple import sort_numbers
result = sort_numbers([3, 1, 2]) # -> [1, 2, 3]
items = [3, 1, 2]
result = sorted(items)
sort_words(items)
Returns a new list of words sorted alphabetically, ignoring case.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
items
|
list
|
List of words. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
list |
list
|
Sorted list of words. |
Example
from py_simple import sort_words
result = sort_words(["banana", "Apple", "cherry"])
# -> ["Apple", "banana", "cherry"]
items = ["banana", "Apple", "cherry"]
result = sorted(items, key=str.lower)
sum_all(items)
Adds up every number in a list, including numbers inside nested lists.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
items
|
list
|
List of numbers, possibly with nested lists. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
int |
int
|
Total of all numbers. |
Example
from py_simple import sum_all
result = sum_all([1, [2, 3], 4]) # -> 10
items = [1, [2, 3], 4]
result = sum(item for sublist in items
for item in (sublist if isinstance(sublist, list)
else [sublist]))
unique_items(items)
Returns a new list with duplicates removed, keeping the original order.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
items
|
list
|
List that may contain duplicates. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
list |
list
|
List with duplicates removed, order preserved. |
Example
from py_simple import unique_items
result = unique_items([1, 2, 2, 3]) # -> [1, 2, 3]
items = [1, 2, 2, 3]
result = []
for item in items:
if item not in result:
result.append(item)