Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I have a list of arrays something like this:
list:
[[['23456', '23456']], [['459687', '201667'],['769687', '203416']]
How can I remove the the nested [] to have a list of list something like this:
list:
[['23456', '23456'], ['459687', '201667'],['769687', '203416']]
Any idea?
new_list = []
for sub_list in nested_list:
if type(sub_list[0]) == list:
for potential_list in sub_list:
if type(potential_list) == list:
new_list.append(potential_list)
elif type(sub_list[0]) == str:
new_list.append(sub_list)
else:
print(type(sub_list)) # if you get here, you have even more weird nesting than in your example
This will handle your example but won't handle nesting deeper than the example. If you need deeper nesting create a function similar to the following but use recursion
Related
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 12 months ago.
Improve this question
I am currently working with different input types -
My input could either look like this:
[term1, term2, term3,...]
or
[[term1, term2], term3]
or
[[term1, term2], [term3, term4]]
I would like to find a way to flatten it to a format over which I could loop in order to use the terms.
My code at the moment looks like this:
for entry in initial_list:
if type(entry) is str:
do_something(entry)
elif type(entry) is list:
for term in entry:
do_something(term)
The goal would be to perform the flattening in a more Pythonic way, using list comprehension or mapping, instead of explicit for loops.
You're very close. It might however be better to use isinstance. Try:
result = list()
for entry in initial_list:
if isinstance(entry, str):
result.append(entry)
elif isinstance(entry, list):
for term in entry:
result.append(term)
A more concise approach is:
for entry in initial_list:
for term in ([entry] if isinstance(entry, str) else entry):
do_something(term)
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 1 year ago.
Improve this question
I don't understand inner workings of dictionary comprehension loop. How could I change this to a for loop?
s_f={node:-1 for node in adj_list.keys()}
It's equivalent to this:
s_f = {}
for node in ajd_list.keys():
s_f[node] = -1
A dictionary comprehension is equivalent to assigning to the key of the resulting dictionary each time through the loop, just as a list comprehension is equivalent to calling append() on the resulting list in the loop.
s_f={node:-1 for node in adj_list.keys()}
is the same as
sf = {}
for node in adj_list.keys():
sf[node] = -1
keep in mind that comprehension is usually faster than a for loop, especially with large data.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I have a list of list as follows
my_list = [['val1','12'],['val2', 'disabled'],['apple', 10],['banana', 20]]
i would like to convert my_list into dictionary such that my_dict looks like
my_dict = {'val1':'12','val2':'disabled','apple':10, 'banana:20}
Just issue dict(my_list). The dict constructor accepts any iterable of two-element iterables.
You could write your own code like:
my_dict = {}
for i in my_list:
my_dict[i[0]] = i[1]
print(my_dict)
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I have the following list:
a=['c1','c2','c3']
And I would like to get to:
b=[sametext['c1'],sametext['c2'],sametext['c3']]
I've tried to make list in list but I'm not able to get any result? How can I get to b?
In [sametext['c1'],sametext['c2'],sametext['c3']], is sametext a dictionary which contains a mapping of some sort?
If that's the case then the way to do it will be with this list comprehension:
b = [sametext[x] for x in a]
Without list comprehensions:
b=[]
for x in a:
b.append(sametext[x])
If by sametext all you mean is some constant operation, like adding a prefix then similar to the first approach the way would be:
b = [f"yourprefix_{x}]" for x in a]
b= [ele+'some_text' for ele in a]
To make change in-place
a[:] = [ele+'some_text' for ele in a]
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 months ago.
Improve this question
How do I print certain elements of a list for example:
list1=["0","0","0","0","0","0","Element1","0","0","0","0"]
Is there any simple way to print only Element1 that specifies that you should not print out anything that is equal to 0.
Use a list comprehension or (as in this example) a generator expression to filter out the "0" items, and loop through the filtered list:
for item in (x for x in list1 if x != "0"):
print(item)
This prints all items that are not "0".