LEC 5 Collection List
LEC 5 Collection List
>>> a is b
False
>>> [1, 2, 3, 4] == [4, 1, 3, 2]
False
List elements can be accessed by index
• Individual elements in a list can be accessed using an
index in square brackets.
• This is exactly analogous to accessing individual
characters in a string.
• List indexing is zero-based as it is with strings.
>>> a = ['foo', 'bar', 'baz', 'qux', 'quux', 'corge']
List Indices
Slicing also works. If a is a list, the
expression a[m:n] returns
the portion of a from index m to, but not including,
index n:
>>> a = ['foo', 'bar', 'baz', 'qux', 'quux', 'corge’]
>>> a[2:5]
• Lists are mutable, meaning their elements can be changed unlike string
or tuple.
• We can use the assignment operator (=) to change an item or a range of items.
x = []
n = input("enter length")
for i in range(1, int(n)):
k=input("enter value")
x.append(k) # push your entered value
print(x)
List Comprehension
Normal manipulation using for loop:
pow2 = []
for x in range(10):
pow2.append(2 ** x)