Loading...

Python - Python List

Python List
Author Ony | April 14, 2022 | Original ref Pykafe class note

A list is a data structure in Python that is a mutable, or changeable, ordered sequence of elements. Each element or value that is inside of a list is called an item. Just as strings are defined as characters between quotes, lists are defined by having values between square brackets [ ].


1. This is how we write the python list in function and testing it

def hello_world():
    ''' This function returns 'Hello World' '''
    return 'Hello World'


2. Test the function

def test_hello_world():
    assert hello_world() == 'Hello World'


3. Add to the List

def add_to_list(my_value, my_list):
    '''
    Given a value and a list this function
    returns a list with the value added to it
    '''
    return my_list.append(my_value)


4. Test the function

def test_add_to_list():
    my_value = 5
    my_list = [1, 2, 3, 4]
    assert add_to_list(my_value, my_list) == [1, 2, 3, 4, 5]


5. Get Item from a List

def get_index_from_list(my_index, my_list):
    '''
    Given an index (integer) and a list this function will
    return the item at that index in the given list.
    If index is not an integer return None
    Remember Python indexes starts at 0
    '''

    if isinstance(my_index, int):
        return my_list[my_index]


6. Test the function

def test_get_index_from_list():
    my_list = ['1', 'Baucau', 'Aileu', 5, 'Cars']

    assert get_index_from_list(2, my_list) == 'Aileu'
    assert get_index_from_list('hello', my_list) == None


7. Check list for value

def is_in_list(my_value, my_list):
    '''
    Given a value and a list this function checks if the value can be found in the list
    and returns True or False
    '''
    pass


8. Test the function

def test_is_in_list():
    my_value = 3
    not_my_value = 4
    my_list = [1, 2, 3]

    assert is_in_list(my_value, my_list) == True
    assert is_in_list(not_my_value, my_list) == False


9. List Length

def get_length(my_list):
    '''
    Given a list this function
    returns the number of items in the list
    '''
    pass


10. Test the function

def test_get_length():
    my_list = [1, 2, 3]
    assert get_length(my_list) == 3


11. Merge two lists

def merge_lists(first_list, second_list):
    '''
    Given two list this function returns a single list which contains the items of the
    first list followed by the items of the second list
    '''
    pass


12. Test the function

def test_merge_lists():

    first_list = [1, 2, 3]
    second_list = [4, 5, 6]
    assert merge_lists(first_list, second_list) == [1, 2, 3, 4, 5, 6]


13. Converting a list to string

def list_to_string(my_list):
    '''
    Given a list the is funciton returns a string which includes the items of the list
    separated by single spaces
    '''
    pass


14. Test the function

def test_list_to_string():
    my_list = ['one', 'two', 'three']
    assert list_to_string(my_list) == "one two three"


15. Count items in list

def count_occurrences(my_list):
    '''
    This function will return a dictionary containing
    the number of occurrences of a item in a given list.
    '''
    pass


16. Test the function

def test_count_occurrences():
    my_list = ['a', 'a', 'a', 'b', 'b', 'b', 'c', 'c']
    assert count_occurrences(my_list) == {'a': 3, 'b': 3, 'c': 2}


17. Strings and lists

def string_to_list(my_string):
    '''
    This function takes a string and returns that string as a list.
    If a the given value is not a string. The function returns None.
    '''
    pass


18. Test the function

def test_string_to_list():
    assert string_to_list('hello world') == [
        'h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd']
    assert string_to_list(5) == None
    assert string_to_list({'hello': 'world'}) == None

0 Comments

Related Post

There are no posts related to this blog!