How to remove "sub-lists" from a list? [duplicate] - python

This question already has answers here:
How do I make a flat list out of a list of lists?
(34 answers)
Closed 3 years ago.
I am having trouble creating a function since I want to be able to refer to the tuple and not the list which contains the tuples. Hence I have come to the conclusion that I want to get rid of the inner square brackets.
I have a list similar to this:
List=[[(1,2),(3,4),(5,6)],[(1,2),(5,7),(3,8)],[...],[...]]
So the question I am asking is how can I remove the inner [ ] so that I can just produce a single list of tuples.
Also, I am not sure if I am allowed to ask another question, but how would i also delete duplicates (x,y) entries in my new list?
I have not provided code for this since I know the problem for the code I have and I believe I would confuse people by including it. If however, you wish to see the code, or want me to clarify anything please let me know.

I think this has been asked and answered on here multiple times. The solution to the flattening problem would be as follows:
new_list = [tupl for l in List for tupl in l]

Related

Python List as List Index [duplicate]

This question already has answers here:
Unpacking tuples/arrays/lists as indices for Numpy Arrays
(4 answers)
Closed 3 years ago.
Really not sure the right question to ask for this, but is it possible to have a list as the index of a list?
Ex:
pixelAddr=[50,50] # list
img[pixelAddr[0], pixelAddr[1]]=[255,255,255] # This is the way I know
# Is something like this possible? I get syntax errors when I try it...
img[*pixelAddr]=[255,255,255]
Btw, using python 3.7
when you do: img[pixelAddr[0], pixelAddr[1]] you are actually just re-packing the indices as a tuple so that is really all you need:
pixelAddr=(50,50) # NOTE THESE ARE ROUND PARENTHASIS
img[pixelAddr]=[255,255,255]
# or
pixelAddrList = [50,50]
img[tuple(pixelAddr)]=[255,255,255]

for loop, and url.remove() only affecting every other entry [duplicate]

This question already has answers here:
How to remove items from a list while iterating?
(25 answers)
Closed 4 years ago.
Getting some confusing behaviour when running a for loop and removing entries from a list (cleaning out invalid urls):
urls = ['http://a.com/?mail=a#b.com','mailto:a#a.com', 'mailto:a#b.com', 'mailto:a#c.com', 'mailto:a#d.com']
for s in urls:
if '#' in s and '?' not in s:
urls.remove(s)
print(urls)
The output is:
['mailto:a#b.com', 'mailto:a#d.com']
It is consistently every other entry, so I'm assuming my understanding of python is not correct.
I looked into list comprehension with Python and ended up with:
urls = [s for s in urls if not ('?' not in s and '#' in s)]
This does what I want it to.
Is that the best way, can someone explain the behaviour, because I don't get it.
Thanks
The problem with your first solution is that you iterate over an object while deleting entries from it. The topic is discussed here for example: How to remove items from a list while iterating?
If you are trying to remove from list while iterating over, take a copy and iterate. urls[:] takes a copy of urls and you iterate over that. This prevents some unexpected situations that occur when iterating through the original list:
urls = ['http://a.com/?mail=a#b.com','mailto:a#a.com', 'mailto:a#b.com', 'mailto:a#c.com', 'mailto:a#d.com']
for s in urls[:]:
if '#' in s and '?' not in s:
urls.remove(s)
print(urls)
But, I would rather prefer the list-comprehension version of yours, that's more concise and pythonic.

what is the difference between [(a,b),(c,d)] and [[a,b],[c,d]] and how can I turn one into another in python? [duplicate]

This question already has answers here:
What's the difference between lists enclosed by square brackets and parentheses in Python?
(7 answers)
Closed 6 years ago.
So my question is exactly defined in the title. That is, what is the difference between
[(a,b),(c,d)]
and
[[a,b],[c,d]]
and how I can turn one into another in Python.
This question is partly duplicate but the main part is how to turn one into another which in not duplicate.
A list is that which encloses its contents in a bracket [].
A tuple is that which encloses its contents in a parentheses ().
Lists are mutable (changeable); tuples are not.
Turning a list into a tuple is possible:
Changing a tuple to list:
list(myTuple)
Changing list to tuple:
tuple(myList)

.remove() in Python list breaks a for loop [duplicate]

This question already has answers here:
How to remove items from a list while iterating?
(25 answers)
Closed 6 years ago.
I wrote a for loop that supposedly removes any elements that fits the description.
Example:
for tag in tags:
if tag.lower() in ['ex1', 'ex2', 'ex3']:
tags.remove(tag)
My tags would look like ['EX1', 'EX2', 'ex1', 'ex2', 'ex3', 'ex4', 'ex5'] and I expect to keep only ex4 and ex5
What I noticed is that the for loop would skip some elements, giving me results like ['EX2', 'ex2', 'ex4', 'ex5']
I suspect this being indexing issue, but I'm not sure if that's really the case.
I ended up using a list comprehension, which does the job correctly, but I just want to understand the true reason behind the unexpected behavior.
Python has a hard time iterating through something that is being changed during the iteration. You can instead use a copy:
for tag in tags[:]:
if tag.lower() in ['ex1', 'ex2', 'ex3']:
tags.remove(tag)

python3 for loop dictionary [duplicate]

This question already has an answer here:
Closed 10 years ago.
Possible Duplicate:
whats another way to write python3 zip
whats a better way to write these things I was introduced to these codes but I am not used to seeing them these ways:
(alla,allc,) = (set(s) for s in zip(*animaldictionary.keys()))
how else can you write this
print('\n'.join(['\t'.join((c,str(sum(animaldictionary.get(ac,0)
for a in alla
for ac in ((a,c,),))//12)))
for c in sorted(allc)]))
I'll update my answer with a more comprehensive (no pun intended) result once I get home and have more time to wrap my head around this interesting set of comprehensions. For now you will want to check out the following:
List Comprehensions
Nested List Comprehensions
Sets And Set Comprehensions

Categories

Resources