0% found this document useful (0 votes)
22 views19 pages

Python Lists: Features and Comprehensions

Here are the list comprehensions to solve the above problems: 1. [x for x in list if x%2 != 0] 2. [item[0] for item in lists] 3. [element for sublist in list for element in sublist]

Uploaded by

pranavr12s22021
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views19 pages

Python Lists: Features and Comprehensions

Here are the list comprehensions to solve the above problems: 1. [x for x in list if x%2 != 0] 2. [item[0] for item in lists] 3. [element for sublist in list for element in sublist]

Uploaded by

pranavr12s22021
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd

Collections-List

Collections in Python are containers that are used to store collections


of data, for example, list, dict, set, tuple etc. These are built-in
collections.
• A list is a kind of collection
• A collection allows us to put many values in a single variable
• A collection is nice because we can carry many values around in
one convenient package.
• List constants are surrounded by square brackets and the elements in
the list are separated by commas
• A list element can be any Python object(the elements can be of
varying types) – even another list
• A list can be empty (Also a list can contain any number of objects,
from zero to as many as your computer’s memory will allow)
• Lists are ordered.
• List elements can be accessed by index.
• Lists are mutable.
• Lists are dynamic.
• Lists can contain any arbitrary objects & Lists can be
nested to arbitrary depth.
The order in which you specify the element when you define a list
is an innate characteristic of that list and is maintained for that list’s
lifetime
Lists Are Ordered:

>>> a = ['foo', 'bar', 'baz', 'qux’]


>>> b =['baz', 'qux', 'bar', 'foo’]
>>> a == b
False

>>> 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]

['baz', 'qux', 'quux']


Lists can be nested to arbitrary depth

L = ['a', 'b', ['cc', 'dd', ['eee', 'fff']], 'g', 'h']


Lists are mutable

• 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.

# Correcting mistake values in a list


odd = [2, 4, 6, 8]
# change the 1st item
odd[0] = 1
print(odd)
# change 2nd to 4th items
odd[1:4] = [3, 5, 7]
print(odd)
Lists are dynamic

x = []
n = input("enter length")
for i in range(1, int(n)):
k=input("enter value")
[Link](k) # push your entered value

print(x)
List Comprehension
Normal manipulation using for loop:

pow2 = []
for x in range(10):
[Link](2 ** x)

Using List Comprehension

pow2 = [2 ** x for x in range(10)]


print(pow2)
List Comprehension –It is an elegant and concise
way to create a new list from an existing list in
Python.

A list comprehension consists of an expression


followed by for statement inside square brackets.
• In maths, the common ways to describe lists (or sets, or
tuples, or vectors) are:
• S = {x² : x in {0 ... 9}}
• V = (1, 2, 4, 8, ..., 2¹²)
• M = {x | x in S and x even}
• In other words, you'll find that the above definitions tell you
the following:
• S is a sequence that contains values between 0 and 9
included, and each value is raised to the power of two.

• The sequence V, on the other hand, contains the value 2


that is raised to a certain power x. The power x starts from
0 and goes till 12.

• Lastly, the sequence M contains only the even elements


from the sequence S.
• S = [x**2 for x in range(10)]
• V = [2**i for i in range(13)]
• M = [x for x in S if x % 2 == 0]
List Comprehension-Applications
Qn1. filter out values divisible by 2.
[Link] all the very first element of list as
elements inside a list.
Qn3. Flatten a given list.

You might also like