Electron microscopy
 
PythonML
Cheatsheet of List
- Python Automation and Machine Learning for ICs -
- An Online Book -
Python Automation and Machine Learning for ICs                                                           http://www.globalsino.com/ICs/        


Chapter/Index: Introduction | A | B | C | D | E | F | G | H | I | J | K | L | M | N | O | P | Q | R | S | T | U | V | W | X | Y | Z | Appendix

=================================================================================

Table 3443a. Cheatsheet of list A.

DogDogsheep
.append(Dog)
DogDogsheepDog
DogDogsheep
.count(Dog)
2
DogDogsheep
.copy()
DogDogsheep
DogDogsheep
.index(sheep)
2
DogDogsheep
.reverse()
sheepDogDog
DogDogsheep
.remove(sheep)
DogDog
DogDogsheep
.insert(1, cat)
DogcatDogsheep
DogDogsheep
.pop(1)
Dogsheep
DogDogsheep
.pop()
DogDog
DogDogsheep
.clear()  

Table 3443b. Cheatsheet of list B.

Applications Details
Remove an/duplicate/all Item from a List  
Empty list  

       

Table 3443c. Cheatsheet of lists.

Package/Method Description  Code Example
B = A[:] To clone a list A and assign the result to list B such that changes to B do not affect A. This method uses slicing to create a new list that is a copy of A, ensuring that B is a separate object in memory.  
A = B  When you use the statement A = B, it does not clone or copy the list. Instead, it makes A refer to the same list object that B refers to. After this assignment, A and B are just two names for the same list in memory. Any changes made to the list through either A or B will be reflected in both because they are referring to the same object.  
B[:] = A This statement assumes B has already been initialized. It copies all elements from A into B, replacing B's original contents. This is a way to copy the contents of A into an existing list B, but not a typical method for cloning a new list.  
append() The `append()` method is used to add an element to the end of a list.

fruits = ["apple", "banana", "orange"]

fruits.append("mango") print(fruits)

copy() The `copy()` method is used to create a shallow copy of a list.

my_list = [1, 2, 3, 4, 5]

new_list = my_list.copy() print(new_list)

# Output: [1, 2, 3, 4, 5]

count() The `count()` method is used to count the number of occurrences of a specific element in a list.

my_list = [1, 2, 2, 3, 4, 2, 5, 2]

count = my_list.count(2) print(count)

# Output: 4

del The `del` statement is used to remove an element from list. `del` statement removes the element at the specified index.

my_list = [10, 20, 30, 40, 50]

del my_list[2] # Removes the element at index 2 print(my_list)

# Output: [10, 20, 40, 50]

extend() The `extend()` method is used to add multiple elements to a list. It takes an iterable (such as another list, tuple, or string) and appends each element of the iterable to the original list.

fruits = ["apple", "banana", "orange"]

more_fruits = ["mango", "grape"]

fruits.extend(more_fruits)

print(fruits)

# Output: ['apple', 'banana', 'orange', 'mango', 'grape']

Indexing Indexing in a list allows you to access individual elements by their position. In Python, indexing starts from 0 for the first element and goes up to `length_of_list - 1`.

my_list = [10, 20, 30, 40, 50]

print(my_list[0])

# Output: 10 (accessing the first element)

print(my_list[-1])

# Output: 50 (accessing the last element using negative indexing)

insert() The `insert()` method is used to insert an element.

my_list = [1, 2, 3, 4, 5]

my_list.insert(2, 6)

print(my_list)

# Output: [1, 2, 6, 3, 4, 5]

Modifying a list You can use indexing to modify or assign new values to specific elements in the list.

my_list = [10, 20, 30, 40, 50]

my_list[1] = 25 # Modifying the second element

print(my_list)

# Output: [10, 25, 30, 40, 50]

pop() `pop()` method is another way to remove an element from a list in Python. It removes and returns the element at the specified index. If you don't provide an index to the `pop()` method, it will remove and return the last element of the list by default

my_list = [10, 20, 60, 40, 50]

removed_element = my_list.pop(2) # Removes and returns the element at index 2

print(removed_element)

# Output: 60

print(my_list)

# Output: [10, 20, 40, 50]

my_list = [10, 20, 60, 40, 50]

removed_element = my_list.pop() # Removes and returns the last element

print(removed_element)

# Output: 50

print(my_list)

# Output: [10, 20, 60, 40]

remove() To remove an element from a list. The `remove()` method removes the first occurrence of the specified value.

my_list = [10, 20, 30, 40, 50]

my_list.remove(30) # Removes the element 30

print(my_list)

# Output: [10, 20, 40, 50]

reverse() The `reverse()` method is used to reverse the order of elements in a list

my_list = [1, 2, 3, 4, 5]

my_list.reverse() print(my_list)

# Output: [5, 4, 3, 2, 1]

Slicing You can use slicing to access a range of elements from a list.

my_list = [1, 2, 3, 4, 5]

print(my_list[1:4])

# Output: [2, 3, 4] (elements from index 1 to 3)

print(my_list[:3])

# Output: [1, 2, 3] (elements from the beginning up to index 2)

print(my_list[2:])

# Output: [3, 4, 5] (elements from index 2 to the end)

print(my_list[::2])

# Output: [1, 3, 5] (every second element)

sort() The `sort()` method is used to sort the elements of a list in ascending order. If you want to sort the list in descending order, you can pass the `reverse=True` argument to the `sort()` method.

my_list = [5, 2, 8, 1, 9]

my_list.sort()

print(my_list)

# Output: [1, 2, 5, 8, 9]

my_list = [5, 2, 8, 1, 9]

my_list.sort(reverse=True)

print(my_list)

# Output: [9, 8, 5, 2, 1]

 



















































 

 

 

 

 

=================================================================================