Movatterモバイル変換


[0]ホーム

URL:


Open In App
Next Article:
How To Find the Length of a List in Python
Next article icon

We are given a list that contains multiple sublists, and our task is to iterate over each of these sublists and access their elements.For example, if we have a list like this: [[1, 2], [3, 4], [5, 6]], then we need to loop through each sublist and access elements like 1, 2, 3, and so on.

Using Nested For Loops

This is the most straightforward way to iterate through each sublist and access individual items.

Python
a=[[1,2,3],[4,5,6],[7,8,9]]foriina:forjini:print(j,end=' ')print()

Output
1 2 3 4 5 6 7 8 9

Explanation:

  • Outer loop iterates through each sublist.
  • Inner loop prints each item in the sublist.
  • end=' ' prints items on the same line, followed by print()for a new line per sublist.

Using List Comprehension

If our goal is to flatten a nested list into a single list, list comprehension offers a concise solution.

Python
a=[[1,2],[3,4],[5,6]]b=[iforjinaforiinj]print(b)

Output
[1, 2, 3, 4, 5, 6]

Explanation:

  • List comprehension flattens the nested list into a single list.
  • "b" contains all elements from "a" in one dimension.

Using enumerate() on Nested Lists

We can useenumerate() to track indices while iterating, which is helpful when we want to know the position of each sublist.

Python
a=[['Python','Java'],['C++','C#'],['Go','Rust']]fori,ginenumerate(a,start=1):print(f"Group{i}:{g}")

Output
Group 1: ['Python', 'Java']Group 2: ['C++', 'C#']Group 3: ['Go', 'Rust']

Explanation:

  • enumerate() tracksindex (i) of each sublist (group).
  • start=1 begins counting from 1.
  • Useful when you need position info along with elements.

Using itertools.chain() Function

itertools.chain() lets us combine multiple iterables into one. It's great for flattening nested lists efficiently, especially with large datasets, as it avoids creating extra lists in memory.

Python
fromitertoolsimportchaina=[[1,2],[3,4],[5,6]]b=list(chain(*a))print(b)

Output
[1, 2, 3, 4, 5, 6]

Explanation:

  • chain(*a)unpacks and chains all sublists.
  • Efficient and memory-friendly for large nested lists.
  • Returns a flat list containing all elements.

Also read:matrices,list-comprehension,itertools.chain(),enumerate().


Improve

Similar Reads

We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood ourCookie Policy &Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences

[8]ページ先頭

©2009-2025 Movatter.jp