DAY 12(CODE INFINITY)
Today we start new topic lists. Let me give you a brief introduction to 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.
List index: Each item/element in a list has its own unique index. This index can be used to access any item from the list. The first item has index [0], this is same like we done in string slicing just like that we do negative indexing also my short trick for negative indexing is add len(variable) before the negative index given and the positive index appear then do same like we do in positive indexing.
You can also check whether an item is present in the list using conditional expression let me show you an example:
colors = ["Red", "Green", "Blue", "Yellow", "Green"]
if "Orange" in colors:
print("Orange is present.")
else:
print("Orange is absent.")
This way you can find any item in list. last one is list comprehension this is used for creating new list from other iterable like list tuple dictionaries and even in arrays and strings. One example for this is:
names = ["Milo", "Sarah", "Bruno", "Anastasia", "Rosa"]
namesWith_O = [item for item in names if "o" in item]
print(namesWith_O)
Screenshot of the repel:
![]() |
Link of the repel:
https://replit.com/@shwetaantil97/-Introduction-to-Lists
Comments