Python List: Exercise - 78 with Solution
Write a Python program to split a given list into two parts where the length of the first part of the list is given.
Sample Solution:
Python Code:
def split_two_parts[n_list, L]:
return n_list[:L], n_list[L:]
n_list = [1,1,2,3,4,4,5, 1]
print["Original list:"]
print[n_list]
first_list_length = 3
print["\nLength of the first part of the list:",first_list_length]
print["\nSplited the said list into two parts:"]
print[split_two_parts[n_list, first_list_length]]
Sample Output:
Original list: [1, 1, 2, 3, 4, 4, 5, 1] Length of the first part of the list: 3 Splited the said list into two parts: [[1, 1, 2], [3, 4, 4, 5, 1]]
Flowchart:
Visualize Python code execution:
The following tool visualize what the computer is doing step-by-step as it executes the said program:
Python Code Editor:
Have another way to solve this solution? Contribute your code [and comments] through Disqus.
Previous: Write a Python program to decode a run-length encoded given list.
Next: Write a Python program to remove the K'th element from a given list, print the new list.
Python: Tips of the Day
Unknown Arguments Using *arguments:
If your function can take in any number of arguments then add a * in front of the parameter name:
def myfunc[*arguments]: for a in arguments: print a myfunc[a] myfunc[a,b] myfunc[a,b,c]
Given a nested 2D list, the task is to split the nested list into two lists such that first list contains first elements of each sublists and second list contains second element of each sublists. Method #1: Using map, zip[]
Python3
ini_list
=
[[
1
,
2
], [
4
,
3
], [
45
,
65
], [
223
,
2
]]
print
["initial
list
",
str
[ini_list]]
res1, res2
=
map
[
list
,
zip
[
*
ini_list]]
print
["final lists", res1, "\n", res2]
Output:
initial list [[1, 2], [4, 3], [45, 65], [223, 2]] final lists [1, 4, 45, 223] [2, 3, 65, 2]
Method #2: Using list comprehension
Python3
ini_list
=
[[
1
,
2
], [
4
,
3
], [
45
,
65
], [
223
,
2
]]
print
["initial
list
",
str
[ini_list]]
res1
=
[i[
1
]
for
i
in
ini_list]
res2
=
[i[
0
]
for
i
in
ini_list]
print
["final lists",
str
[res1], "\n",
str
[res2]]
Output:
initial list [[1, 2], [4, 3], [45, 65], [223, 2]] final lists [2, 3, 65, 2] [1, 4, 45, 223]
Method #3: Using operator.itemgetter[]
Python3
from
operator
import
itemgetter
ini_list
=
[[
1
,
2
], [
4
,
3
], [
45
,
65
], [
223
,
2
]]
print
["initial
list
",
str
[ini_list]]
res1
=
list
[
map
[itemgetter[
0
], ini_list]]
res2
=
list
[
map
[itemgetter[
1
], ini_list]]
print
["final lists",
str
[res1], "\n",
str
[res2]]
Output:
initial list [[1, 2], [4, 3], [45, 65], [223, 2]] final lists [1, 4, 45, 223] [2, 3, 65, 2]
Method #4 : Using extend[] method
Python3
ini_list
=
[[
1
,
2
], [
4
,
3
], [
45
,
65
], [
223
,
2
]]
print
[
"initial list"
,
str
[ini_list]]
x
=
[]
for
i
in
ini_list:
x.extend[i]
res1
=
[]
res2
=
[]
for
i
in
range
[
0
,
len
[x]]:
if
[i
%
2
=
=
0
]:
res1.append[x[i]]
else
:
res2.append[x[i]]
print
[
"final lists"
,
str
[res1],
"\n"
,
str
[res2]]
Output
initial list [[1, 2], [4, 3], [45, 65], [223, 2]] final lists [1, 4, 45, 223] [2, 3, 65, 2]