Work with lists

Here below is not right about Pandas, but usefull in some Pandas contexts.

Search if elements from a list are in another list

Chek if elements from List2 are in List1:

[code]import collections

List1 = ['Aaa', 'Bbbb', 'Cccc', 'Dddd', 'Eeee', 'Ffff', 'Gggg', 'Hhhh']
List2 = ['Eeee', 'Cccc']

print(List1)
print(List2)

check = all(item in List1 for item in List2)

print(check)[/code]

 Or:

[code]import collections

List1 = ['Aaa', 'Bbbb', 'Cccc', 'Dddd', 'Eeee', 'Ffff', 'Gggg', 'Hhhh']
List2 = ['Bbbb', 'Aaa', 'Cccc', 'Dddd', 'Eeee', 'Ffff', 'Hhhh', 'Gggg']

print(List1)
print(List2)

def iCompare(c1, c2):
if(collections.Counter(c1)==collections.Counter(c2)):
return 'Yes'
else:
return 'No'

info = iCompare(List1, List2)

print(info)[/code]

 Extract common elements from several lists

[code]List1 = ['Aaa', 'Aaa', 'Bbbb', 'Cccc', 'Dddd', 'Eeee', 'Ffff', 'Gggg', 'Hhhh']
List2 = ['Eeee', 'Cccc', 'Cccc']
List3 = ['Cccc', 'Bbbb', 'Eeee']

print('List1:', List1)
print('List2:', List2)
print('List3:', List3)

Compare = sorted(list(set(List1) & set(List2) & set(List3) ))

print('\nCompare:', Compare)[/code]

 Extract no-common elements from several lists

[code]List1 = ['Aaa', 'Aaa', 'Bbbb', 'Cccc', 'Dddd', 'Eeee', 'Ffff', 'Gggg', 'Hhhh']
List2 = ['Eeee', 'Cccc', 'Cccc']

print('List1:', List1)
print('List2:', List2)

Compare = sorted(list(set(List1) - set(List2) ))

print('\nCompare:', Compare)[/code]

Concatenate 2 fields in list/sentence

[code]Assemblage = list(zip(df['field1'].tolist(), df['field2'].tolist()))
b = []
for a in Assemblage:
b.append('Blablabla... ' + str(list(a)[0]) + ' ...Blablabla... ' + list(a)[1] + ' ...Blablabla.')
for i in b:
print(i)[/code]

List all file from a directory in a Python list

[code]repertoireCheck = 'E:/_CheckTables/'

listTxtFile = []

for path, subdirs, files in os.walk(repertoireCheck):
for name in files:
print(os.path.join(path, name))
listTxtFile.append(os.path.join(path, name))

print(listTxtFile)
print(len(listTxtFile))[/code]

Split a big list in smaller sub-lists with a defined total

Here 750 records for example:

[code]SubListsSize = 750
SubLists = [MyBigList[x:x+SubListsSize] for x in range(0, len(MyBigList), SubListsSize)]

for SubList in SubLists:
print(SubList)[/code]

Better: split a big list in smaller sub-lists with a defined total in a function

[code]def split_list(my_list, size_max):
sublists = []
for i in range(0, len(my_list),size_max):
sublist = liste[i:i+size_max]
sublists.append(sublist)
return sublists

sublists = split_list(MyPersonalList, 10)[/code]