Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 16

Further List Operations

Iterating through a List using Loop


languages = ['Python', 'Swift', 'C++']
# iterating through the list
for language in languages:
print(language)
Using Membership operator in List
languages = ['Python', 'Swift', 'C++']
print('C' in languages) # False
print('Python' in languages) # True
Python List Comprehension
• A list comprehension consists of an expression followed by the for
statement inside square brackets.
numbers = [number*number for number in range(1, 6)]
print(numbers)
# Output: [1, 4, 9, 16, 25]
• List comprehension is an elegant way to define and create lists based
on existing lists.
• List comprehension is generally more compact and faster than normal
functions and loops for creating list.
• However, we should avoid writing very long list comprehensions in
one line to ensure that code is user-friendly.
• Remember, every list comprehension can be rewritten in for loop, but
every for loop can’t be rewritten in the form of list comprehension.
Example 1
h_letters = [ letter for letter in 'human' ]
print( h_letters)
[‘h’,’u’,’m’,’a’,’n’]
Example 2
• Conditionals in List Comprehension
number_list = [ x for x in range(21) if x % 2 == 0]
print(number_list)
Output: [2,4,6,8,10,12,14,16,18,20]
• Nested if with list comprehension
num_list = [y for y in range(100) if (y % 2 == 0 and y % 5 == 0)]
print(num_list)
[0,10,20,30,40,50,60,70,80,90]
• if else with List Comprehension
obj = ["Even" if i%2==0 else "Odd" for i in range(10)]
print(obj)
numbers=[x*x for x in range(1,6)]
numbers
OR
numbers = []
for x in range(1, 6):
numbers.append(x * x)
numbers
Flattening a List in Python
• In Python, a list of lists (or cascaded lists) resembles a two-
dimensional array - although Python doesn't have a concept of the
array as in C or Java.
• Hence, flattening such a list of lists means getting elements of sublists
into a one-dimensional array-like list.
• [[1,2,3],[4,5,6],[7,8,9]] is flattened to [1,2,3,4,5,6,7,8,9]
Using List Comprehension
nestedlist = [ [1, 2, 3, 4], ["Ten", "Twenty", "Thirty"], [1.1, 1.0E1, 1+2j,
20.55, 3.142]]
flatlist=[element for sublist in nestedlist for element in sublist]
print(flatlist)
Splitting list into chunks
• To split up a list into parts of the same size, zip() function can be used
with iter() function
• The function takes in iterables as arguments and returns an iterator.
• This iterator generates a series of tuples containing elements from
each iterable (list, set, tuples, file etc.)
• The zip() function returns a zip object, which is an
iterator of tuples where the first item in each passed
iterator is paired together, and then the second item in
each passed iterator are paired together etc.
• Note: If the passed iterators have different lengths, the
iterator with the least items decides the length of the
new iterator.
Syntax: zip(iterator1, iterator2, iterator3 ...)
where, iterator1, iterator2, iterator3 are the
objects that are joined together
a = ("John", "Charles", "Mike")
b = ("Jenny", "Christy", "Monica", "Vicky")
x = zip(a, b) #zip object
list(x)
Output:
numbers = [1, 2, 3]
letters = ['a', 'b', 'c']
zipped = zip(numbers, letters)
zipped # Holds an iterator object
#<zip object at 0x7fa4831153c8>
type(zipped)
#<class 'zip'>
list(zipped)

You might also like