How to create sublist from list in python

Such a list of lists could be constructed using a list comprehension:

In [17]: seq=[1,2,3,4,5,6,7,8]
In [18]: [seq[i:i+3] for i in range[0,len[seq],3]]
Out[18]: [[1, 2, 3], [4, 5, 6], [7, 8]]

There is also the grouper idiom:

In [19]: import itertools
In [20]: list[itertools.izip_longest[*[iter[seq]]*3]]
Out[20]: [[1, 2, 3], [4, 5, 6], [7, 8, None]]

but note that missing elements are filled with the value None. izip_longest can take a fillvalue parameter as well if something other than None is desired.

list1+=[list2] -- noting the brackets this time -- is equivalent to list1.append[list2]. My highest priority when writing code is readability, not speed. For this reason, I would go with list1.append[list2]. Readability is subjective, however, and probably is influenced greatly by what idioms you're familiar with.

Happily, in this case, readability and speed seem to coincide:

In [41]: %timeit list1=[1,2,3]; list1.append[list2]
1000000 loops, best of 3: 612 ns per loop

In [42]: %timeit list1=[1,2,3]; list1+=[list2]
1000000 loops, best of 3: 847 ns per loop

Syntax of List [Literal Expression to Create a List]

[x0, x1, x2 etc]

x = [1, 2, "m"]
print[x]

list can have items of different types.

Count Items

len[list] Return the count of number of items.
x = [0, 1, 2]
print[len[x] == 3]

Check Existence

x in listcheck if exist.
x = [0, 1, 2]
print[2 in x]

x = ["a", "b", "c"]
print["b" in x]
x not in list return true if not in list.
x = [0, 1, 2]
print[3 not in x]

x = ["a", "b", "c"]
print["m" not in x]

Get one Item

list[i] Return item in list at index i.
x = ["a", "b", "c"]
print[x[1] == "b"]

Index starts at 0. Best to think of index as between items. The left of first item is 0.

Negative index counts from right.

x = ["a", "b", "c"]
print[x[-1] == "c"]

Set/Change One Item

list[i] = new_value Change a item's value.
x = [1, 2, 3]
x[2] = "b"
print[x == [1, 2, "b"]]

Delete One Item

del list[i] Delete one item.
x = ["a", "b", "c"]
del x[1]
print[x == ["a", "c"]]

Get Sublist

list[i:j] Return a sublist from index i to j.

Note: index start at 0. Item at index 0 means first item. Ending index does not include itself.

x = [0, 1, 2, 3, 4, 5]
print[x[1:4] == [1, 2, 3]]
list[:j] From beginning to j. list[i:] From i to the end, including the end item.
x = [0, 1, 2, 3, 4, 5]
print[x[3:] == [3, 4, 5]]

Delete Sublist

del list[i:j]delete the items between i to j [not including j]. Same as list[i:j] = []del list[i:j:k]delete the items between i to j [not including j], with step k.

Replace Sublist

list[i:j] = list2 Replace i to j items by the contents of the list2
x = [1, 2, 3, 4, 5]
x[2:4] = [99, 100]
print[x == [1, 2, 99, 100, 5]]
list[i:j:k] = list2Replace items list[i:j:k] by content of list2. [list2 must have same number of items]
e = list[range[20]]
e[0:10:2] = ["a"] * 5
print[e]

Nested Lists

Lists can be nested arbitrarily. Example:

x = [3, 4, [7, 8]]

Append extra bracket to get item of nested list.

x = [3, 4, [7, 8]]
print[x[2][1]]

Join Lists

Note: there is also list methods append. see Python: List Methods

list1 + list2 Join 2 lists into 1.
x = ["a", "b"] + [7, 6]
print[x]

How do you create a sublist from a list in Python?

Step 2 : take one sublist which is empty initially. Step 3 : use one for loop till length of the given list. Step 4 : Run a loop from i+1 to length of the list to get all the sub arrays from i to its right. Step 5 : Slice the sub array from i to j.

How do you split a list into multiple lists in Python?

Python Program to Split a List Into Evenly Sized Chunks.
Using a for loop and range[] method, iterate from 0 to the length of the list with the size of chunk as the step..
Return the chunks using yield . list_a[i:i+chunk_size] gives each chunk..

How do you add to a sublist in Python?

The + operator when used with a list simply adds new elements to each of the list items. In the below example we find that even a list itself can be used as a new element to be added to the existing lift. Also the existing elements in the list can be of varying length.

Chủ Đề