Repository navigation
Expand file tree
/
Copy pathpython revise.txt
More file actions
58 lines (39 loc) · 1.5 KB
/
Copy pathpython revise.txt
File metadata and controls
58 lines (39 loc) · 1.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
Q1) zip vs enumerate
zip and enumerate are both built-in functions in Python that serve different purposes.
zip function:
zip takes two or more iterables as input and returns an iterator that generates tuples containing elements from the input iterables, paired together.
Example:
python
Copy code
list1 = [1, 2, 3]
list2 = ['a', 'b', 'c']
zipped = zip(list1, list2)
result = list(zipped)
print(result) # Output: [(1, 'a'), (2, 'b'), (3, 'c')]
zip stops creating tuples when the shortest input iterable is exhausted.
enumerate function:
enumerate is used to iterate over a sequence (such as a list) while keeping track of the index of the current item.
Example:
python
Copy code
my_list = ['apple', 'banana', 'orange']
for index, value in enumerate(my_list):
print(index, value)
# Output:
# 0 apple
# 1 banana
# 2 orange
You can also specify a start value for the index using the start parameter of enumerate.
In summary:
Use zip when you want to combine elements from two or more iterables into tuples.
Use enumerate when you want to iterate over elements of a sequence while keeping track of their indices.
# create a list of names
names = ['sravan', 'bobby', 'ojaswi', 'rohith', 'gnanesh']
# create a list of subjects
subjects = ['java', 'python', 'R', 'cpp', 'bigdata']
# create a list of marks
marks = [78, 100, 97, 89, 80]
# use enumerate() and zip() function
# to iterate the lists
for i, (names, subjects, marks) in enumerate(zip(names, subjects, marks)):
print(i, names, subjects, marks)