# List operations in Python

`Python's list manipulation is powerful and convenient (as opposed to Java)Say nothing about simple, regular operations (this is not an introductory tutorial) and introduce a few very specific examplesAdd to`
```# Append to End(append)
li = [1, 2, 3, 4, 5]
li.append(6)
print('li = %s' % li)
# Output: li = [1, 2, 3, 4, 5, 6]

# Append a list(extend)
li2 = ['a', 'b', 'c', 'd', 'e']
li.extend(li2)
print('li = %s' % li)
# Output: li = [1, 2, 3, 4, 5, 6, 'a', 'b', 'c', 'd', 'e']

# Lists and lists can also be added
li_plus = li + li2
print('li_plus = %s' % li_plus)
# Output: li_plus = [1, 2, 3, 4, 5, 6, 'a', 'b', 'c', 'd', 'e', 'a', 'b', 'c', 'd', 'e']```
`delete`
```# Delete elements from list
li = [1, 2, 3, 4, 5]
li.remove(3)
print('li = %s' % li)
# Output: li = [1, 2, 4, 5]
# Delete elements by index
del li[3]
print('li = %s' % li)
# Output: li = [1, 2, 4]```

`Interception (also known as slicing)Format: [start:end:step].Remember this format and you can imagine it`
```li = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]# Take one every two elements
li2 = li[::2]
print('li2 = %s' % li2)
# Output: li2 = [0, 2, 4, 6, 8, 10]

# Last element
element = li[-1]
print('element = %s' % element)
# Output: element = 10

# Copy (a new object after copy)
li2 = list(li)
print('li2 = %s' % li2)
# Output: li2 = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]```
`Extract and delete (pop)`
```li = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

# Extract and delete the last element
li.pop()    # Equivalent to li.pop(-1)
print('li = %s' % li)
# Output: li = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

# Extract and delete the first element (or n Elements)
li.pop(0)
print('li = %s' % li)
# Output: li = [1, 2, 3, 4, 5, 6, 7, 8, 9]```

`sort`
```# Positive Ordering
li = [3, 1, 5, 8, 0, 9, 2, 6, 7, 4]
li.sort()
print('li = %s' % li)
# Output: li = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

# Reverse order
li = [3, 1, 5, 8, 0, 9, 2, 6, 7, 4]
li.sort(reverse=True)
print('li = %s' % li)
# Output: li = [9, 8, 7, 6, 5, 4, 3, 2, 1, 0]

# Flip (not in reverse order)
li = [3, 1, 5, 8, 0, 9, 2, 6, 7, 4]
li.reverse()
print('li = %s' % li)
# Output: li = [4, 7, 6, 2, 9, 0, 8, 5, 1, 3]```
`List ResolutionHow to efficiently create a new list based on an existing listFormat: [expression for iter_val in iterable]`
```# Square each item in the list
li = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
li2 = [i**2 for i in li]
print('li2 = %s' % li2)
# Output: li2 = [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]

# Only numbers greater than 5 are squared.The expression can also be if Sentence
li = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
li2 = [i**2 if i > 5 else i for i in li]
print('li2 = %s' % li2)
# Output: li2 = [0, 1, 2, 3, 4, 5, 36, 49, 64, 81]```
`Format: [expr for iter_var in iterable if cond_expr]`
```# Only take non None Elements of
li = [0, 1, 2, 3, 4, 5, None, 6, 7, 8, 9]
li2 = [i for i in li if i is not None]
print('li2 = %s' % li2)
# Output: li2 = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]```
`Format: [expr for iter_var in iterable for var in iter_var]`
```# Expand a two-dimensional list
li = [[0, 1, 2], [3, 4, 5, 6], [7, 8, 9]]
li2 = [j for i in li for j in i]
print('li2 = %s' % li2)
# Output: li2 = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]```
`Connect elements in the list (join)`
```li = ['aaa', 'bbb', 'ccc', 'ddd']
li2 = ', '.join(li)
print('li2 = %s' % li2)
# Output: li2 = aaa, bbb, ccc, ddd```

Tags: Python Java

Posted on Sun, 12 Jul 2020 10:52:13 -0400 by jim.davidson