Products
Home
Customers
Use cases
PUBLIC Nested lists python
Stack Overflow
Search…
Ask Question
Log
Tagsin Sign up
asked 7 years, 7 months ago
Users
viewed 125,893 times
Jobs 10
active 9 months ago
TEAMS
What’s this?
9
Blog
Can anyone tell me how can I call for indexes in a nested list?
Q&A for Work
Generally I just write:
Sing Me A Song of Stack Overflow: A Musical
Tail Call Optimization
for i in range (list)
Featured on Meta
but what if I have a list with nested lists as below:
Nlist = [[2,2,2],[3,3,3],[4,4,4]...] Custom Filters release announcement
and I want to go through the indexes of each one separately?
Unicorn Meta Zoo #5: Interview with Cesar M
python
82 people chatting
share improve this question
edited Sep 29 '18 at 17:31
Python
bphi 15 mins ago - holdenweb
2,455 3 14 24
asked Nov 18 '11 at 21:08
user1040563
1,574 8 25 34 Linked
2 comparing lists python
You need to rewrite your question and make it clear. Your use of “indexes” is suspect; perhaps you meant 0 Why seems Python to crash when I create
“items”? – tzot Nov 19 '11 at 21:36 a nested list of objects?
0 Using a list of strings in an if statement
This is a question about walking nested lists, the other is about comparing nested lists. – Kev Nov 20 '11 at
15:37 0 I am trying to read a word from a column in
a text file in python but get the error: 'list'
object is not callable
6 Answers Related
active oldest votes
4033 How to merge two dictionaries in a single
expression?
3236 How do I check if a list is empty?
20 4308 Calling an external command in Python
5192 What are metaclasses in Python?
2721 Finding the index of an item given a list
containing it in Python
If you really need the indices you can just do what you said again for the inner list:
3631 How can I safely create a nested directory?
l = [[2,2,2],[3,3,3],[4,4,4] 5281 Does Python have a ternary conditional
for i1 in xrange(len(l)): operator?
for i2 in xrange(len(l[i1])):
print i1, i2, l[i1][i2] 2658 How to make a flat list out of list of lists
3474 How do I list all files of a directory?
But it is more pythonic to iterate through the list itself:
3603 Does Python have a string 'contains'
for inner_l in l: substring method?
for item in inner_l:
print item Hot Network Questions
Is it possible to spoof an IP address to an exact
If you really need the indices you can also use enumerate :
number?
How to calculate a conditional PDF in
for i1, inner_l in enumerate(l): mathematica?
for i2, item in enumerate(inner_l):
print i1, i2, item, l[i1][i2] What is the difference between a historical drama
and a period drama?
share improve this answer What happens if the limit of 4 billion files was
exceeded in an ext4 partition?
answered Nov 18 '11 at 21:10
Can a USB hub be used to access a drive from 2
Claudiu
devices?
131k 128 403 598
Creating patterns
Speeding up thousands of string parses
this was helpful but i hate how you used variables "l', i and 1. so freakin' hard to read and differentiate I dont Recursive conversion from ExpandoObject to
know why people do this for their examples. – Aspen Nov 15 '18 at 17:10 Dictionary<string, object>
How to iterate equal values with the standard
library?
Machine Learning Golf: Multiplication
List comprehensions in Mathematica?
Do I need to be legally qualified to install a Hive
5 smart thermostat?
Why did moving the mouse cursor cause
Windows 95 to run more quickly?
Try this setup: Why weren't Gemini capsules given names?
Are there any extinct phonemes in Russian?
a = [["a","b","c",],["d","e"],["f","g","h"]]
Why is the saxophone not common in classical
repertoire?
To print the 2nd element in the 1st list ("b"), use print a[0][1] - For the 2nd element in 3rd list
Should I warn my boss I might take sick leave
("g"): print a[2][1]
How frequently do Russian people still refer to
The first brackets reference which nested list you're accessing, the second pair references the others by their patronymic (отчество)?
item in that list.
Are there advantages in writing by hand over
typing out a story?
share improve this answer
Will electrically joined dipoles of different lengths,
edited Dec 17 '14 at 14:59
at right angles, behave as a multiband antenna?
4444
What can a novel do that film and TV cannot?
3,351 9 25 43
Does Evolution Sage proliferate Blast Zone when
answered Dec 17 '14 at 14:07 played?
JAG
PhD: When to quit and move on?
51 1 1
Has chattel slavery ever been used as a criminal
punishment in the USA since the passage of the
Thirteenth Amendment?
Question feed
You can do this. Adapt it to your situation:
for l in Nlist:
for item in l:
print item
share improve this answer
answered Nov 18 '11 at 21:11
lc2817
3,174 12 34
The question title is too wide and the author's need is more specific. In my case, I needed to
extract all elements from nested list like in the example below:
Example:
input ‐> [1,2,[3,4]]
output ‐> [1,2,3,4]
The code below gives me the result, but I would like to know if anyone can create a simpler
answer:
def get_elements_from_nested_list(l, new_l):
if l is not None:
e = l[0]
if isinstance(e, list):
get_elements_from_nested_list(e, new_l)
else:
new_l.append(e)
if len(l) > 1:
return get_elements_from_nested_list(l[1:], new_l)
else:
return new_l
Call of the method
l = [1,2,[3,4]]
new_l = []
get_elements_from_nested_list(l, new_l)
share improve this answer
answered May 4 '18 at 20:37
lfvv
856 8 12
-1
n = [[1, 2, 3], [4, 5, 6, 7, 8, 9]]
def flatten(lists):
results = []
for numbers in lists:
for numbers2 in numbers:
[Link](numbers2)
return results
print flatten(n)
Output: n = [1,2,3,4,5,6,7,8,9]
share improve this answer
edited May 17 '18 at 16:11
user6655984
answered May 17 '18 at 16:11
John Lliuya Martiarena
1
This snippet is an unnecessary re-invention of chain.from_iterable – user6655984 May 17 '18 at 16:13
Indeed it is... – Attersson May 17 '18 at 16:18
-1
I think you want to access list values and their indices simultaneously and separately:
l = [[2,2,2],[3,3,3],[4,4,4],[5,5,5]]
l_len = len(l)
l_item_len = len(l[0])
for i in range(l_len):
for j in range(l_item_len):
print(f'List[{i}][{j}] : {l[i][j]}' )
share improve this answer
answered Oct 2 '18 at 19:20
Shivansh Chaudhri
6 5
protected by Community ♦ Oct 2 '18 at 19:21
Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be
removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).
Would you like to answer one of these unanswered questions instead?
Not the answer you're looking for? Browse other questions tagged python or ask your own question.
STACK OVERFLOW
Questions
Jobs
Developer Jobs Directory
Salary Calculator
Help
Mobile
Disable Responsiveness
PRODUCTS
Teams
Talent
Advertising
Enterprise
COMPANY
About
Press
Work Here
Legal
Privacy Policy
Contact Us
STACK EXCHANGE
NETWORK
Technology
Life / Arts
Culture / Recreation
Science
Other
Blog
Facebook
Twitter
LinkedIn
site design / logo © 2019 Stack ExchangeBy
Inc;using our site, you
user contributions acknowledge
licensed that you
under cc by-sa have
3.0 with read and
attribution understand
required. our Cookie Policy, Privacy Policy, and our Terms of Service.
rev 2019.7.2.34200