Skip to main content
Tidied up code formatting
Source Link
Joe
  • 48k
  • 37
  • 166
  • 265

If you're using Python 3, you can you use the star before a variable (on the left side of an assignment) to have it be a list in unpacking.

# Example 1: a is 1 and b is [2, 3]

a, *b = [1, 2, 3]

# Example 2: a is 1, b is [2, 3], and c is 4

a, *b, c = [1, 2, 3, 4]

# Example 3: b is [1, 2] and c is 3

*b, c = [1, 2, 3]

# Example 4: a is 1 and b is []

a, *b = [1]

# Example 1: a is 1 and b is [2, 3] a, *b = [1, 2, 3] # Example 2: a is 1, b is [2, 3], and c is 4 a, *b, c = [1, 2, 3, 4] # Example 3: b is [1, 2] and c is 3 *b, c = [1, 2, 3] # Example 4: a is 1 and b is [] a, *b = [1] 

If you're using Python 3, you can you use the star before a variable (on the left side of an assignment) to have it be a list in unpacking.

# Example 1: a is 1 and b is [2, 3]

a, *b = [1, 2, 3]

# Example 2: a is 1, b is [2, 3], and c is 4

a, *b, c = [1, 2, 3, 4]

# Example 3: b is [1, 2] and c is 3

*b, c = [1, 2, 3]

# Example 4: a is 1 and b is []

a, *b = [1]

If you're using Python 3, you can you use the star before a variable (on the left side of an assignment) to have it be a list in unpacking.

# Example 1: a is 1 and b is [2, 3] a, *b = [1, 2, 3] # Example 2: a is 1, b is [2, 3], and c is 4 a, *b, c = [1, 2, 3, 4] # Example 3: b is [1, 2] and c is 3 *b, c = [1, 2, 3] # Example 4: a is 1 and b is [] a, *b = [1] 
Source Link
Winston Chuen-Shih Yang
Winston Chuen-Shih Yang

If you're using Python 3, you can you use the star before a variable (on the left side of an assignment) to have it be a list in unpacking.

# Example 1: a is 1 and b is [2, 3]

a, *b = [1, 2, 3]

# Example 2: a is 1, b is [2, 3], and c is 4

a, *b, c = [1, 2, 3, 4]

# Example 3: b is [1, 2] and c is 3

*b, c = [1, 2, 3]

# Example 4: a is 1 and b is []

a, *b = [1]