0% found this document useful (0 votes)
33 views4 pages

3 1-Lists

Uploaded by

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

3 1-Lists

Uploaded by

AB
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

7/18/24, 9:34 AM 3.1-Lists.

ipynb - Colab

keyboard_arrow_down Introduction To Lists

Lists are ordered, mutable collections of items.


They can contain items of different data types.

keyboard_arrow_down Video Outline:

1. Introduction to Lists
2. Creating Lists
3. Accessing List Elements
4. Modifying List Elements
5. List Methods
6. Slicing Lists
7. Iterating Over Lists
8. List Comprehensions
9. Nested Lists
10. Practical Examples and Common Errors

lst=[]
print(type(lst))

<class 'list'>

names=["Krish","Jack","Jacob",1,2,3,4,5]
print(names)

['Krish', 'Jack', 'Jacob', 1, 2, 3, 4, 5]

mixed_list=[1,"Hello",3.14,True]
print(mixed_list)

[1, 'Hello', 3.14, True]

### Accessing List Elements

fruits=["apple","banana","cherry","kiwi","gauva"]

print(fruits[0])
print(fruits[2])
print(fruits[4])
print(fruits[-1])

apple
cherry
gauva
gauva

print(fruits[1:])
print(fruits[1:3])

['banana', 'cherry', 'kiwi', 'gauva']


['banana', 'cherry']

## Modifying The List elements


fruits

['apple', 'banana', 'cherry', 'kiwi', 'gauva']

fruits[1]="watermelon"
print(fruits)

['apple', 'watermelon', 'cherry', 'kiwi', 'gauva']

fruits[1:]="watermelon"

https://colab.research.google.com/drive/11Faup-xQ_1t0LtzlCQBbYyWPZnOWzUKW#printMode=true 1/4
7/18/24, 9:34 AM 3.1-Lists.ipynb - Colab

fruits

['apple', 'w', 'a', 't', 'e', 'r', 'm', 'e', 'l', 'o', 'n']

fruits=["apple","banana","cherry","kiwi","gauva"]

## List Methods

fruits.append("orange") ## Add an item to the end


print(fruits)

['apple', 'banana', 'cherry', 'kiwi', 'gauva', 'orange']

fruits.insert(1,"watermelon")
print(fruits)

['apple', 'watermelon', 'banana', 'banana', 'cherry', 'kiwi', 'gauva', 'orange']

fruits.remove("banana") ## Removing the first occurance of an item


print(fruits)

['apple', 'watermelon', 'banana', 'cherry', 'kiwi', 'gauva', 'orange']

## Remove and return the last element


popped_fruits=fruits.pop()
print(popped_fruits)
print(fruits)

orange
['apple', 'watermelon', 'banana', 'cherry', 'kiwi', 'gauva']

index=fruits.index("cherry")
print(index)

fruits.insert(2,"banana")
print(fruits.count("banana"))

fruits

['apple', 'watermelon', 'banana', 'banana', 'cherry', 'kiwi', 'gauva']

fruits.sort() ## SSorts the list in ascending order

fruits

['apple', 'banana', 'banana', 'cherry', 'gauva', 'kiwi', 'watermelon']

fruits.reverse() ## REverse the list

fruits

['watermelon', 'kiwi', 'gauva', 'cherry', 'banana', 'banana', 'apple']

fruits.clear() ## Remove all items from the list

print(fruits)

[]

## Slicing List
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
print(numbers[2:5])
print(numbers[:5])
print(numbers[5:])
i t( b [ 2])
https://colab.research.google.com/drive/11Faup-xQ_1t0LtzlCQBbYyWPZnOWzUKW#printMode=true 2/4
7/18/24, 9:34 AM 3.1-Lists.ipynb - Colab
print(numbers[::2])
print(numbers[::-1])

[3, 4, 5]
[1, 2, 3, 4, 5]
[6, 7, 8, 9, 10]
[1, 3, 5, 7, 9]
[10, 9, 8, 7, 6, 5, 4, 3, 2, 1]

numbers[::3]

[1, 4, 7, 10]

numbers[::-2]

[10, 8, 6, 4, 2]

### Iterating Over List

for number in numbers:


print(number)

1
2
3
4
5
6
7
8
9
10

## Iterating with index


for index,number in enumerate(numbers):
print(index,number)

0 1
1 2
2 3
3 4
4 5
5 6
6 7
7 8
8 9
9 10

## List comprehension
lst=[]
for x in range(10):
lst.append(x**2)

print(lst)

[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]

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

[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]

keyboard_arrow_down List Comprehension

Basics Syantax [expression for item in iterable]

with conditional logic [expression for item in iterable if condition]

Nested List Comprehension [expression for item1 in iterable1 for item2 in iterable2]

### Basic List Comphrension

sqaure=[num**2 for num in range(10)]


print(sqaure)

https://colab.research.google.com/drive/11Faup-xQ_1t0LtzlCQBbYyWPZnOWzUKW#printMode=true 3/4
7/18/24, 9:34 AM 3.1-Lists.ipynb - Colab

[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]

### List Comprehension with Condition


lst=[]
for i in range(10):
if i%2==0:
lst.append(i)

print(lst)

[0, 2, 4, 6, 8]

even_numbers=[num for num in range(10) if num%2==0]


print(even_numbers)

[0, 2, 4, 6, 8]

## Nested List Comphrension

lst1=[1,2,3,4]
lst2=['a','b','c','d']

pair=[[i,j] for i in lst1 for j in lst2]

print(pair)

[[1, 'a'], [1, 'b'], [1, 'c'], [1, 'd'], [2, 'a'], [2, 'b'], [2, 'c'], [2, 'd'], [3, 'a'], [3, 'b'], [3, 'c'], [3, 'd'], [4, 'a'], [4, '

## List Comprehension with function calls


words = ["hello", "world", "python", "list", "comprehension"]
lengths = [len(word) for word in words]
print(lengths) # Output: [5, 5, 6, 4, 13]

[5, 5, 6, 4, 13]

keyboard_arrow_down Conclusion

List comprehensions are a powerful and concise way to create lists in Python. They are syntactically compact and can replace more verbose
looping constructs. Understanding the syntax of list comprehensions will help you write cleaner and more efficient Python code.

Start coding or generate with AI.

https://colab.research.google.com/drive/11Faup-xQ_1t0LtzlCQBbYyWPZnOWzUKW#printMode=true 4/4

You might also like