Lists and Tuples Syntax
|
Lists are created with [ ] |
|
Tuples are created with ( ) |
|
Returns 1st element of L (1) |
|
Returns 1st element of T (10) |
|
Returns 2nd to 4th element of L ([2, 3, 4]) |
|
Returns 2nd to 4th element of T ((20, 30, 40)) |
|
Returns 1st to 2nd last element of L skipping one at a time ([1, 3]) |
|
Returns 1st to 2nd last element of L skipping one at a time ((10, 30)) |
|
Assigns 22 to 2nd element of L (L == [1, 22, 3, 4, 5]) |
|
ERROR: You can't assing anything to tuples |
|
Assigns 11 and 22 to 1st and 2nd element of L respectively (L == [11, 22, 3, 4, 5]) |
Lists are mutable and Tuples are NOT mutable
Lists - Methods
|
|
|
Returns a concatenated with b (['a', 'b', 'c', 1, 3, 2]) |
|
Returns True if 'c' is in the list a and False otherwise (True) |
|
Returns the number of elements in a (3) |
|
Appends 'd' to the end of the list a (a == ['a', 'b', 'c', 'd']) |
a.extend(['d', 'e', 'f'])
|
Appends every element of the iterable to the end of a (a == ['a', 'b', 'c', 'd', 'e', 'f']) |
|
Inserts 'd' to index 1 of a (a == ['a', 'd', 'b', 'c']) |
|
Returns the last element of the list and deletes it from the list. ('c') |
|
Returns 2nd element of a and removes it from the list ('b') |
|
Removes first occurrence of 'b' in a (a == ['a', 'c']) |
|
Clears the list entirely (a == []) |
|
Returns the index of the first occurrence of 'b' (1) |
|
Returns the number of occurrences of 'b' in a (1) |
|
Returns a sorted version of b ([1, 2, 3]) |
|
Reverses the list a (['c', 'b', 'a']) |
|
Returns a copy of a |
The copy() method returns a list identical to the original, but with a different ID. It means that they are allocated in different places of memory.
Tuple - Methods
|
|
|
Returns a concatenated version of t1 and t2 |
|
Returns True if 2 is in t2 and False otherwise (True) |
|
Returns the number of elements in t1 (3) |
|
Returns the number of occurrences of 2 in t2 (1) |
|
Returns the index of the 1st occurrence of 1 (0) |
Lists - Loops 1
a = ['one' , 'two', 'three']
for i in a:
print(i)
|
Lists - Loops 2
a = ['one' , 'two', 'three']
for i in range(len(a)):
print(f"a[{i}] == {a[i]}")
|
a[0] == one
a[1] == two
a[2] == three
|
|
|