List of tuples from original list
Question:
I have a list [6, 63, 131, 182, 507, 659, 669]
and would like to create a new list of pairs that looks like this:
[(7,63),(64,131),(132,182),(183,507),(508,659),(660,669)]
When I use the below code:
list = [6, 63, 131, 182, 507, 659, 669]
line_num = 0
for idx,i in enumerate(list):
print(list[idx]+1,list[idx+1])
I get below error:
---------------------------------------------------------------------------
IndexError Traceback (most recent call last)
5 pass
6 else:
----> 7 print(city_index[idx]+1,city_index[idx+1])
IndexError: list index out of range
How do I end the loop before I get an error?
Answers:
l = [6, 63, 131, 182, 507, 659, 669]
new_l = []
for i in range(1, len(l)):
new_l.append((l[i-1] + 1, l[i]))
print(new_l)
output:
[(7, 63), (64, 131), (132, 182), (183, 507), (508, 659), (660, 669)]
You loop until the length of the list – 1 to avoid out of range:
lst = [6, 63, 131, 182, 507, 659, 669]
pairs = []
for i in range(len(lst)-1):
pairs.append((lst[i]+1, lst[i+1]))
Consider using zip
instead. This frees you from worrying about list indices (for the most part).
lst = [6, 63, 131, 182, 507, 659, 669]
for x, y in zip(lst, lst[1:]):
print(x+1, y)
If you don’t like the idea of making a shallow copy of lst
, use itertools.islice
instead.
from itertools import islice
lst = [6, 63, 131, 182, 507, 659, 669]
for x, y in zip(lst, islice(lst, 1, None)):
print(x+1, y)
Or, use itertools.pairwise
, introduced in Python 3.10
for x, y in pairwise(lst):
print(x+1, y)
nums = [6, 63, 131, 182, 507, 659, 669]
pairs = [(nums[i] + 1, nums[i + 1]) for i in range(len(nums) - 1)]
You could do this in a list comprehension and stop the loop at len(nums) - 1
instead of len(nums)
. You probably don’t want to call a variable list
because that’s a special word in python.
Use slicing, and it returns everything of the message but the last element ie :-1
list = [6, 63, 131, 182, 507, 659, 669]
line_num = 0
for idx,i in enumerate(list[:-1]):
print(list[idx]+1,list[idx+1])
I have a list [6, 63, 131, 182, 507, 659, 669]
and would like to create a new list of pairs that looks like this:
[(7,63),(64,131),(132,182),(183,507),(508,659),(660,669)]
When I use the below code:
list = [6, 63, 131, 182, 507, 659, 669]
line_num = 0
for idx,i in enumerate(list):
print(list[idx]+1,list[idx+1])
I get below error:
---------------------------------------------------------------------------
IndexError Traceback (most recent call last)
5 pass
6 else:
----> 7 print(city_index[idx]+1,city_index[idx+1])
IndexError: list index out of range
How do I end the loop before I get an error?
l = [6, 63, 131, 182, 507, 659, 669]
new_l = []
for i in range(1, len(l)):
new_l.append((l[i-1] + 1, l[i]))
print(new_l)
output:
[(7, 63), (64, 131), (132, 182), (183, 507), (508, 659), (660, 669)]
You loop until the length of the list – 1 to avoid out of range:
lst = [6, 63, 131, 182, 507, 659, 669]
pairs = []
for i in range(len(lst)-1):
pairs.append((lst[i]+1, lst[i+1]))
Consider using zip
instead. This frees you from worrying about list indices (for the most part).
lst = [6, 63, 131, 182, 507, 659, 669]
for x, y in zip(lst, lst[1:]):
print(x+1, y)
If you don’t like the idea of making a shallow copy of lst
, use itertools.islice
instead.
from itertools import islice
lst = [6, 63, 131, 182, 507, 659, 669]
for x, y in zip(lst, islice(lst, 1, None)):
print(x+1, y)
Or, use itertools.pairwise
, introduced in Python 3.10
for x, y in pairwise(lst):
print(x+1, y)
nums = [6, 63, 131, 182, 507, 659, 669]
pairs = [(nums[i] + 1, nums[i + 1]) for i in range(len(nums) - 1)]
You could do this in a list comprehension and stop the loop at len(nums) - 1
instead of len(nums)
. You probably don’t want to call a variable list
because that’s a special word in python.
Use slicing, and it returns everything of the message but the last element ie :-1
list = [6, 63, 131, 182, 507, 659, 669]
line_num = 0
for idx,i in enumerate(list[:-1]):
print(list[idx]+1,list[idx+1])