Slicing a list using a variable, in Python

0 votes
I want to slice the following list using a variable:

a=[1,2,3,4,5,6,7]

Is it possible?
Jul 2 in Python by Nisa
• 1,090 points
16 views

1 answer to this question.

0 votes

Yes its possible. Use the following piece of code:

EXAMPLE:

a=[1,2,3,4,5,6,7]
b=slice(a[1:5])
print(b)

OUTPUT: 

slice(None, [2, 3, 4, 5], None)

answered Jul 2 by Wajiha
• 1,120 points

Related Questions In Python

0 votes
1 answer

How to list only text files in a directory using python?

Well, you are using a complex way. ...READ MORE

answered Feb 3 in Python by Omkar
• 67,120 points
129 views
+4 votes
7 answers

Splitting a List into chunks in Python

Here's a generator that yields the chunks ...READ MORE

answered Apr 13, 2018 in Python by Nietzsche's daemon
• 4,260 points
3,061 views
+1 vote
4 answers

Count the frequency of an item in a python list

To count the number of appearances: from collections ...READ MORE

answered Oct 18, 2018 in Python by tinitales
162 views
0 votes
1 answer

How can I convert a list of dictionaries from a CSV into a JSON object in Python?

You could try using the AST module. ...READ MORE

answered Apr 17, 2018 in Python by anonymous
551 views
0 votes
1 answer

Adding elements to a list in python

Use extend() instead: l = [5, 7, 12, ...READ MORE

answered Apr 23, 2018 in Python by Nietzsche's daemon
• 4,260 points
24 views
0 votes
1 answer

Sorting a list of strings in Python

Use the sort function mylist.sort() READ MORE

answered May 29, 2018 in Python by Nietzsche's daemon
• 4,260 points
23 views
0 votes
1 answer

How to Print a List in Python

ou are using Python 2.x syntax with ...READ MORE

answered Aug 31, 2018 in Python by charlie_brown
• 7,710 points
57 views
+1 vote
1 answer

Creating a range of dates in Python

Yes, you can do it as follows: import ...READ MORE

answered Jul 4 in Python by Wajiha
• 1,120 points
10 views
0 votes
1 answer

time.sleep() in Python

Sleep function plays a very important role ...READ MORE

answered Jul 9 in Python by Wajiha
• 1,120 points
15 views