1big_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]
2x = 4
3list_of_lists = [big_list[i:i+x] for i in range(0, len(big_list), x)]
4# [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]]
1foo = "A B C D"
2bar = "E-F-G-H"
3
4# the split() function will return a list
5foo_list = foo.split()
6# if you give no arguments, it will separate by whitespaces by default
7# ["A", "B", "C", "D"]
8
9bar_list = bar.split("-", 3)
10# you can specify the maximum amount of elements the split() function will output
11# ["E", "F", "G"]
1def split(txt, seps):
2 default_sep = seps[0]
3 # we skip seps[0] because that's the default separator
4 for sep in seps[1:]:
5 txt = txt.replace(sep, default_sep)
6 return [i.strip() for i in txt.split(default_sep)]
7
8
9>>> split('ABC ; DEF123,GHI_JKL ; MN OP', (',', ';'))
10['ABC', 'DEF123', 'GHI_JKL', 'MN OP']
1list = [11, 18, 19, 21]
2
3length = len(list)
4
5middle_index = length // 2
6
7first_half = list[:middle_index]
8second_half = list[middle_index:]
9
10print(first_half)
11print(second_half)
1string = "this is a string" # Creates the string
2splited_string = string.split(" ") # Splits the string by spaces
3print(splited_string) # Prints the list to the console
4# Output: ['this', 'is', 'a', 'string']