Advanced List concepts

List Comprehension

When it comes to working with lists in Python, there's a powerful tool at your disposal called list comprehension. This concise syntax allows you to generate lists based on existing iterables with ease, making your code more readable and efficient.

List comprehension offers a concise way to create lists by applying an expression to each item in an existing iterable, optionally with filtering conditions.


# Traditional Approach:
squares = []
for x in range(5):
    squares.append(x**2)

# Explanation:
# In the traditional approach, we initialize an empty list 'squares'.
# We then loop through numbers from 0 to 4 using the range() function.
# For each number 'x', we calculate its square (x**2) and append it to the 'squares' list.

# List Comprehension:
squares = [x**2 for x in range(5)]

# Explanation:
# With list comprehension, we achieve the same result in a single line.
# We use a compact syntax to iterate over numbers from 0 to 4 and calculate their squares directly,
# creating the list 'squares' in one go.

Condition in List Comprehension
even_num=[num for num in range(1,10) if num % 2 == 0]
print(even_num)


Condition List Comprehension

char=[char for char in word if char in vowel]
print(char)



len(list): this gives the length of the list as output. For example:
numbers = [2, 5, 7, 9]
print(len(numbers))

max(list): returns the item in the list with the maximum value. For example:
numbers = [2, 5, 7, 9]
print(max(numbers))
 
min(list): returns the item in the list with the minimum value. For example:

numbers = [2, 5, 7, 9]
print(min(numbers))
>>> 2

    list(tuple): converts a tuple object a list. For example;

animals = (cat, dog, fish, cow)
print(list(animals))
>>> [cat, dog, fish, cow]

    list.append(element): appends the element to the list. For example;

numbers = [2, 5, 7, 9]
numbers.append(15)
print(numbers)
>>> [2, 5, 7, 9, 15]

    list.pop(index): removes the element at the specified index from the list. For example;

numbers = [2, 5, 7, 9, 15]
numbers.pop(2)
print(numbers)
>>> [2, 5, 9, 15]

    list.remove(element):deletes the element from the list.For example;

values = [2, 5, 7, 9]
values.remove(2)
print(values)
>>> [5, 7, 9]

    list.reverse(): reverses the objects of the list. For example;

values = [2, 5, 7, 10]
values.reverse()
print(values)
>>> [10, 7, 5, 2]

    list.index(element): to get the index value of an element within the list. For example;

animals = ['cat', 'dog', 'fish', 'cow', 'goat']
fish_index = animals.index('fish')
print(fish_index)
>>> 2

    sum(list): to get the sum of all the values in the list, if the values are all numbers (integers or decimals). For example;

values = [2, 5, 10]
sum_of_values = sum(values)
print(sum_of_values)

>>> 17

If the list contains any element that is not a number, such as a string, the sum method would not work. You would get an error saying: "TypeError: unsupported operand type(s) for +: 'int' and 'str'"

list.sort(): to arrange a list of integers, floating point numbers, or strings, in ascending or descending order. For example:

values = [1, 7, 9, 3, 5]
# To sort the values in ascending order:
values.sort()
print(values)

>>> [1, 3, 5, 7, 9]

Another example:

values = [2, 10, 7, 14, 50]
# To sort the values in descending order:
values.sort(reverse = True)
print(values)

No comments:

Post a Comment