A list is a collection of elements stored in a single variable.

Industry observers note that we can access list elements using their index. Indexing starts from 0.

An iterable is an object that allows us to access its elements one by one.

For example, we can access individual characters in a string using indexing.

Similarly, we can access elements in a list:.

Common non-iterable data types:.

This gives a TypeError because an integer is not iterable.

Industry observers note that remember: A dictionary is iterable, and iterating over it directly gives its keys. A set is also iterable, but it does not maintain a guaranteed element order.

Both append() and extend() are list methods used to add elements to a list. However, they work differently.

The append() method adds a single item to the end of a list.

Here, the entire l2 list is added as one element inside l1.

Therefore, the length becomes 4.

The extend() method adds each element from another iterable to the existing list.

Here, the elements of l2 are added individually to l1.

Therefore, the length becomes 6.

The reverse() method reverses the order of elements in a list.

It has been revealed that note: reverse() modifies the original list. It does not return a fresh reversed list.

The sort() method arranges list elements in ascending order by default.

We can use reverse=True to arrange elements in descending order.

We can also sort strings alphabetically.

What happens if we try to sort a list containing both integers and strings?

Python needs to compare elements to decide their sorting order.

But what happens when we compare an integer with a string?

This produces a TypeError because Python does not support the less-than comparison between an integer and a string.

Python uses comparison methods such as lt() to perform less-than comparisons.

Operator overloading means using the same operator to perform different operations depending on the objects involved.

The + operator performs addition for numbers and concatenation for strings and lists.

Python uses special methods to implement these operations.

A list allows duplicate values, whereas a set stores only unique values.

The duplicate values are not stored in a set.

Python supports different operators on lists.

The + operator joins two lists and creates a new list.

The * operator repeats the elements of a list.

The == operator checks whether two lists have the same elements in the same order.

Order matters when comparing lists.

The != operator checks whether two lists are different.

5. Greater Than (>) and Less Than (<).

Python compares lists element by element from left to right.

The first two elements are equal. Python compares the third elements: 30 and 40.

Since 30 is smaller than 40, l1 < l2 returns True.

It has been revealed that string comparisons are case-sensitive. Therefore, 'abcd' and 'ABCD' are different.

List comprehension is a shorter way to create a fresh list from an iterable.

Instead of writing multiple lines using a for loop and append(), we can write everything in a single line.

[expression for item in iterable] Example 1: Creating a list of numbers.

Example 2: Creating a list of squares.

Example 3: Finding even numbers.

We can use an if condition inside list comprehension.

Example 4: Using if-else in List Comprehension.

We can also use an if-else condition to generate different values.

The same logic using a normal for loop:.

Both approaches produce the same output.

📌 Read also:

→ Read original article