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, 2019 in Python by Nisa
• 1,090 points
100 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, 2019 by Wajiha
• 1,950 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, 2019 in Python by Omkar
• 69,030 points
1,063 views
0 votes
1 answer

Write code to create a list of word lengths for the words in original_str using the accumulation pattern and assign the answer to a variable num_words_list.

Hi,  num_words_list = len(original_str.split()) original_str.split() - split words in ...READ MORE

answered May 27 in Python by Niroj
• 66,420 points
1,018 views
0 votes
1 answer
0 votes
1 answer

Is there a way to store this text in a list using selenium (python)

Try using this code snippet to resolve ...READ MORE

answered Aug 24 in Python by Carlos
54 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
25,613 views
+1 vote
7 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
11,989 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
1,966 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, 2019 in Python by Wajiha
• 1,950 points
369 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, 2019 in Python by Wajiha
• 1,950 points
587 views