Change language

Python | Incremental section of a slice in a list

| |

Method # 1: Using Loops
This is a brute force method by which this task can be accomplished. We just manually count and increment the counter with each iteration to slice and create a dictionary key.

 ` # Python3 code to demonstrate how it works ` ` # Incremental slice section in list ` ` # Using a loop `   ` # initializing list ` ` test_list ` ` = ` ` [` ` 1 ` `, ` ` 2 ` `, ` ` 3 ` `, ` ` 4 ` `, ` ` 5 ` `, ` ` 6 ` `, ` ` 7 , 8 , 9 , 10 ] ``   # print original list print ( "The original list is:" + str (test_list))   # Incremental slice section in the list # Using a loop res = {} N = 1 strt = 0 while strt & lt;  len (test_list): res [N] = test_list [strt: strt + N] strt + = N N + = 1    # print result print ( "The partitioned dictionary from list is:" + str (res) ) `

Output:

The original list is: [1, 2, 3, 4, 5, 6, 7 , 8, 9, 10]
The partitioned dictionary from list is: {1: [1], 2: [2, 3], 3: [4, 5, 6], 4: [7, 8, 9, 10]}

Method # 2: Using ` enumerate () + slice () + next () + iter () + count () `
A combination of the above functions can be used to accomplish this task. In this, ` next () ` is used to iterate through the list converted to an iterator using ` iter () `` slice () ` slices the list. Count () helps in managing the count, and the enumeration keeps track of the element and the indices in the list.

 ` # Python3 code to demonstrate how it works ` ` # Incremental slice section in list ` ` # Using enumerate () + slice () + next () + iter () + count () ` ` from ` ` itertools ` ` import ` ` islice, count `   ` # initializing list ` ` test_list ` ` = ` ` [` ` 1 ` `, ` ` 2 ` `, ` ` 3 ` `, ` ` 4 ` `, ` ` 5 ` `, ` ` 6 ` `, ` ` 7 ` `, ` ` 8 ` `, ` ` 9 ` `, ` ` 10 ` `] `   ` # printing the original list ` ` print ` ` (` ` "The original list is:" ` ` + ` ` str ` ` (test_list)) `   ` # Incremental slice section in the list ` ` # Using enumerate () + slice () + next () + iter () + count () ` ` res < / code> = {key: val for key, val in enumerate ( iter ( lambda i = iter (test_list), `` c = count ( 1 ): list (islice (i, next (c))), []), 1 )}    # print result print ( "The partitioned dictionary from list is:" + str (res)) `

Output:

The original list is: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
The partitioned dictionary from list is: {1: [1], 2: [2, 3], 3: [4, 5, 6], 4: [7, 8 , 9, 10]}

Shop

Learn programming in R: courses

\$

Best Python online courses for 2022

\$

Best laptop for Fortnite

\$

Best laptop for Excel

\$

Best laptop for Solidworks

\$

Best laptop for Roblox

\$

Best computer for crypto mining

\$

Best laptop for Sims 4

\$

Latest questions

NUMPYNUMPY

Common xlabel/ylabel for matplotlib subplots

NUMPYNUMPY

How to specify multiple return types using type-hints

NUMPYNUMPY

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

NUMPYNUMPY

Flake8: Ignore specific warning for entire file

NUMPYNUMPY

glob exclude pattern

NUMPYNUMPY

How to avoid HTTP error 429 (Too Many Requests) python

NUMPYNUMPY

Python CSV error: line contains NULL byte

NUMPYNUMPY

csv.Error: iterator should return strings, not bytes

Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

How to specify multiple return types using type-hints

Printing words vertically in Python

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries