Manipulating lists in python

From raju

dummy

print list to a file

using f strings:

    fname = 'my_file.txt'
    with open(fname, 'w') as fh:
        for item in my_list:
            fh.write(f"{item}\n")
    

Ref:- https://stackoverflow.com/questions/899103/writing-a-list-to-a-file-with-python

tags | write an array to a file in python

without using f strings:

    with open('my_file.txt', 'w') as fh:
        for item in my_list:
            fh.write("%s\n" % item)
    

print all items in a list that match a string

    matches = [s for s in my_list if my_string in s]
    

Ref :- https://stackoverflow.com/questions/21151174/python-if-list-contains-string-print-all-the-indexes-elements-in-the-list-tha

create an empty list

    a = []
    

Another way is

    a = list()
    

But this is ~4 times slower as shown below:

    $ python -mtimeit "l=[]"
    10000000 loops, best of 3: 0.0274 usec per loop
    
    $ python -mtimeit "l=list()"
    10000000 loops, best of 3: 0.125 usec per loop
    

Ref:- https://stackoverflow.com/questions/2972212/creating-an-empty-list-in-python

Check if a list is empty

    $ cat check_for_empty_list.py
    a = []
    if (not a):
        print("List is empty")
    
    $ python check_for_empty_list.py
    List is empty
    

Check if a variable is a list

convert a list to dictionary

    In [1]: s = ['mat', 'plot', 'lib']
    
    In [2]: d = {k:1 for k in s}; print(d)
    {'mat': 1, 'plot': 1, 'lib': 1}
    
    In [3]: d = {k:len(k) for k in s}; print(d)
    {'mat': 3, 'plot': 4, 'lib': 3}
    

convert two lists to dictionary

    In [1]: a = ['mat', 'plot', 'lib']; b = ['lab', 'ly', 'c']
    
    In [2]: d = {k:v for (k,v) in zip(a, b)}; print(d)
    {'mat': 'lab', 'plot': 'ly', 'lib': 'c'}
    

Convert string to a list of one element

     % python3
    Python 3.5.3 (default, Jan 19 2017, 14:11:04) 
    [GCC 6.3.0 20170118] on linux
    Type "help", "copyright", "credits" or "license" for more information.
    >>> a = 'foo'
    >>> type(a)
    <class 'str'>
    >>> a
    'foo'
    >>> b = [a]
    >>> type(b)
    <class 'list'>
    >>> b
    ['foo']
    

If you apply list() directly, each letter will be an element in the list.

    >>> c = list(a)
    >>> type(c)
    <class 'list'>
    >>> c
    ['f', 'o', 'o']
    

subset a list based on another boolean array

    >>> a=['k', 'a', 'ma', 'raju']
    >>> b = [True, False, True, False]
    >>> a
    ['k', 'a', 'ma', 'raju']
    >>> b
    [True, False, True, False]
    >>> c = [i for (i,v) in zip(a, b) if v]
    >>> c
    ['k', 'ma']
    

Ref: http://stackoverflow.com/questions/18665873/filtering-a-list-based-on-a-list-of-booleans

list indices of all true values of an array

    >>> import numpy as np
    >>> a = np.zeros(shape=10, dtype=bool)
    >>> a = np.zeros(shape=10, dtype=bool); a[3:5] = True; a[7:9] = True; a
    array([False, False, False,  True,  True, False, False,  True,  True, False], dtype=bool)
    >>> from itertools import compress
    >>> b = list(compress(range(len(a)), a)); b
    [3, 4, 7, 8]
    

create a list of given length

    >>> a=[None]*10
    >>> a
    [None, None, None, None, None, None, None, None, None, None]
    >>> a[2] = 4
    >>> a
    [None, None, 4, None, None, None, None, None, None, None]
    >>> a[3] = [1,2,3]
    >>> a
    [None, None, 4, [1, 2, 3], None, None, None, None, None, None]
    

Ref:- http://stackoverflow.com/questions/983699/initialise-a-list-to-a-specific-length-in-python

String for SQL where clause

index of matching elements based on partial string

     % python
    Python 2.7.13 (default, Nov 24 2017, 17:33:09) 
    [GCC 6.3.0 20170516] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    >>> mylist = ["aa123", "bb2322", "aa354", "cc332", "ab334", "333aa"]
    >>> index = [i for i, s in enumerate(mylist) if 'aa' in s]
    >>> index
    [0, 2, 5]
    
    >>> new_list = [mylist[i] for i in index]
    >>> new_list
    ['aa123', 'aa354', '333aa']
    

tags | find the index of matching element in a list

demonstrates | print list of lists

get the last element of a list

Use a[-1] to get the last element of list a.

In general, a[-n] gets the nth-to-last element. So a[-1] gets the last element, a[-2] gets the second to last, etc, all the way down to a[-len(a)], which gets the first element.

    >>> a = ['zero', 'one', 'two', 'three', 'four']
    >>> a[-1]
    'four'
    >>> a[-2]
    'three'
    >>> len(a)
    5
    >>> a[-len(a)]
    'zero'
    

Ref:- https://stackoverflow.com/questions/930397/getting-the-last-element-of-a-list-in-python

useful links

sort elements in a list

tags | sorted reverse

To sort in ascending order, reverse the sorted order, using a custom function to sort

    >>> a = ['baba', 'abab', 'ab', 'a', 'ababab']
    
    >>> sorted(a)
    ['a', 'ab', 'abab', 'ababab', 'baba']
    
    >>> sorted(a, reverse=True)
    ['baba', 'ababab', 'abab', 'ab', 'a']
    
    >>> sorted(a, key=len)
    ['a', 'ab', 'baba', 'abab', 'ababab']
    
    >>> sorted(a, key=len, reverse=True)
    ['ababab', 'baba', 'abab', 'ab', 'a']
    

To sort by name

    from glob import glob
    
    sorted(glob('*.png'))
    

To sort by modification time

    from glob import glob
    import os
    
    sorted(glob('*.png'), key=os.path.getmtime)
    

To sort on size

    from glob import glob
    import os
    
    sorted(glob('*.png'), key=os.path.getsize)
    

break data into chunks

tags | list comprehension, using range

    In [1]: data = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm']
    
    In [2]: chunk_size = 3
    
    In [3]: chunks = [data[x:x+chunk_size] for x in range(0, len(data), chunk_size)]
    
    In [4]: chunks
    Out[4]: [['a', 'b', 'c'], ['d', 'e', 'f'], ['g', 'h', 'i'], ['j', 'k', 'l'], ['m']]
    

combine strings in each list in a list of lists

Given [['a', 'b', 'c'], ['d', 'e', 'f'], ['g', 'h', 'i'], ['j', 'k', 'l'], ['m']] , the task here is to get ['a,b,c', 'd,e,f', 'g,h,i', 'j,k,l', 'm']

    In [1]: chunks = [['a', 'b', 'c'], ['d', 'e', 'f'], ['g', 'h', 'i'], ['j', 'k', 'l'], ['m']]
    
    In [2]: list(map(','.join, chunks))
    Out[2]: ['a,b,c', 'd,e,f', 'g,h,i', 'j,k,l', 'm']
    

combine two lists into one

    In [1]: a = ['k', 'a', 'm', 'a']
       ...: print(a)
    ['k', 'a', 'm', 'a']
    
    In [2]: a + a
    Out[2]: ['k', 'a', 'm', 'a', 'k', 'a', 'm', 'a']
    

items in list a that are also in list b

tags | intersection

    [value for value in a if value in b]
    

Ref:- https://www.geeksforgeeks.org/python-intersection-two-lists/

list slicing

get last n elements

    a[-n:]
    
    >>> a = [1, 2, 3, 4, 5, 6, 7, 8]
    >>> a[-3:]
    [6, 7, 8]
    

get first n elements

    a[:n]
    
    >>> a = [1, 2, 3, 4, 5, 6, 7, 8]
    >>> a[:3]
    [1, 2, 3]
    

get last element

    a[-1]
    
    >>> a = [1, 2, 3, 4, 5, 6, 7, 8]
    >>> a[-1]
    8
    

get first element

    a[0]
    
    >>> a = [1, 2, 3, 4, 5, 6, 7, 8]
    >>> a[0]
    1