Python Lists
Python Lists
- Lists are ordered collection of data items.
- They store multiple items in a single variable.
- List items are separated by commas and enclosed within square brackets [].
- Lists are changeable meaning we can alter them after creation.
Example 1:
lst1 = [1,2,2,3,5,4,6]lst2 = ["Red", "Green", "Blue"]print(lst1)print(lst2)
Output:
[1, 2, 2, 3, 5, 4, 6]['Red', 'Green', 'Blue']
Example 2:
details = ["Abhijeet", 18, "FYBScIT", 9.8]print(details)
Output:
['Abhijeet', 18, 'FYBScIT', 9.8]
As we can see, a single list can contain items of different data types.
List Index
Each item/element in a list has its own unique index. This index can be used to access any particular item from the list. The first item has index [0], second item has index [1], third item has index [2] and so on.
Example:
colors = ["Red", "Green", "Blue", "Yellow", "Green"]# [0] [1] [2] [3] [4]
Accessing list items
We can access list items by using its index with the square bracket syntax []. For example colors[0] will give "Red", colors[1] will give "Green" and so on...
List Comprehension
List comprehensions are used for creating new lists from other iterables like lists, tuples, dictionaries, sets, and even in arrays and strings.
Syntax:
List = [Expression(item) for item in iterable if Condition]
Expression: It is the item which is being iterated.
Iterable: It can be list, tuples, dictionaries, sets, and even in arrays and strings.
Condition: Condition checks if the item should be added to the new list or not.
Example 1: Accepts items with the small letter “o” in the new list
names = ["Milo", "Sarah", "Bruno", "Anastasia", "Rosa"]namesWith_O = [item for item in names if "o" in item]print(namesWith_O)
Output:
['Milo', 'Bruno', 'Rosa']
Example 2: Accepts items which have more than 4 letters
names = ["Milo", "Sarah", "Bruno", "Anastasia", "Rosa"]namesWith_O = [item for item in names if (len(item) > 4)]print(namesWith_O)
Output:
['Sarah', 'Bruno', 'Anastasia']