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
29 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,920 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,660 points
300 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
7,624 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
1,588 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
800 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
48 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
45 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,720 points
98 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,920 points
33 views
0 votes
1 answer

Writing output to new file using xml.etree.ElementTree in python

To write results to a new file, ...READ MORE

answered Jul 25 in Python by Wajiha
• 1,920 points
38 views