0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Python list related utilities

Posted at

Often in Python you work with lists and often you will be doing tasks like finding difference between two lists or intersection between two lists or their union. These are all set operations and all of these operations can be achieved with python sets. In addition you perform tasks like searching for items matching certain patterns within the list. Below are utilities that help with such tasks.

import itertools


def flatten_list_of_lists(list_of_lists):
    return list(itertools.chain.from_iterable(list_of_lists))


def get_list_from_string(the_string_of_lines):
    the_list = the_string_of_lines.split("\n")
    the_list = [item.strip() for item in the_list]
    if "" in the_list:
        the_list.remove("")
    return the_list


def get_chunked_list(original_list, chunk_size: int = 400):
    chunked_list = [original_list[i:i + chunk_size] for i in range(0, len(original_list), chunk_size)]
    return chunked_list


def generate_comma_separated_text(item_list, with_quote=True):
    str_item_list = [str(item).strip() for item in item_list]
    if with_quote:
        comma_seperated_text = ','.join(f"'{item}'" for item in str_item_list)
    else:
        comma_seperated_text = ','.join(str_item_list)
    return comma_seperated_text


def search_list(_list, _text):
    """
    Get elements of a list that contain certain text
    :param _list:
    :param _text:
    :return:
    """
    return [item for item in _list if _text in item]


def get_intersection(list1, list2):
    """
    Get intersection of two lists
    This returns elements that are both in list1 and list2
    :param list1:
    :param list2:
    :return:
    """
    return list(set(list1) & set(list2))


def get_list_diff(list1, list2):
    """
    Get difference between two lists
    This return elements form list1 that are not in list2
    :param list1:
    :param list2:
    :return:
    """
    return list(set(list1) - set(list2))


def print_list_items(mylist):
    for item in mylist:
        print(item)
0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?