Copy a ListRYB_color = ["Red","Yellow",Blue"] Example 1 Copy_RYB = RYB_color.copy() Example 2 Copy_RYB = list(RYB_color)
|
Join Two ListsRYB_color = ["Red","Yellow",Blue"] Second_color = ["Green","Orange","Purple"] Example 1 RYB_Second = RYB_color.extend(Second_color) Example 2 RYB_Second = RYB_color + Second_color
|
The list() ConstructorRYB_color = list(("Red","Yellow",Blue"))
|
TupleA tuple is a collection which is ordered and unchangeable. In Python tuples are written with round brackets. |
Tuple ExampleRYB_color = ("Red","Yellow","Blue") print(RYB_color) >>>('Red','Yellow','Blue')
|
Access Tuple ItemsSame as access list items. Please check "Access list items" in "Python Lists - Part I" cheat sheet. |
Change Tuple ValuesSince tuples are inchangeable, then we cannot change any value. But, we can convert a tuple to a list, change the values that we want within the list and convert the list back into a tuple. Example `RYB_color = ("Red","Yellow","Blue") L = list(RYB_color) L[0] = "Magenta" RYB_color = tuple(X) |
| | Check if Item ExistsSame as checking item witin a list . Please check "Check if Item Exists" in "Python Lists - Part I" cheat sheet. |
Tuple LengthSame as list length. Please check "List Length" in "Python Lists - Part I" cheat sheet. |
Add ItemsOnce a tuple is created, we cannot add items to it. Tuples are unchangeable. |
Create Tuple With One ItemTo create a tuple with only one item, we have add a comma after the item, unless Python will not recognize the variable as a tuple. | Color = ("White",)
|
Remove ItemsTuples are unchangeable, so we cannot remove items from it, but we can delete the tuple completely. | RYB_color = ("Red","Yellow","Blue") del RYB_color
|
Join Two TuplesRYB_color = ("Red","Yellow","Blue") Second_color = ("Orange","Green","Purple") RYB_Second = RYB_color + Second_color
|
The tuple() ConstructorRYB_color = tuple(("Red","Yellow","Blue")) |
|
Created By
Metadata
Comments
No comments yet. Add yours below!
Add a Comment
Related Cheat Sheets
More Cheat Sheets by Nouha_Thabet