This question already has answers here:
How do I make a flat list out of a list of lists?
(34 answers)
Flatten an irregular (arbitrarily nested) list of lists
(51 answers)
Closed 5 years ago.
This is what I came up with but is there a shorter way of doing this? Trying to make a list out of tuple pairs.
x = [[1, 2], [3, 4], [5, 6]]
list1, list2 = zip(*x)
list1=list1+list2
list1=list(list1)
list1.sort()
outcome is [1, 2, 3, 4, 5, 6]
Related
This question already has answers here:
Flatten an irregular (arbitrarily nested) list of lists
(51 answers)
Closed 1 year ago.
For instance, I could have this list:
l = [1, [2, 3], [4, 5, [6, 7], 8]]
and I want to flatten the list like this:
l = [1, 2, 3, 4, 5, 6, 7, 8]
I found some methods online for doing this, but they assume that the list doesn't contain any values outside of sub-lists, or have side effects like reversing the order of elements. What's the best way to do this?
[Edit] I got an existing question recommended, but I couldn't understand what "yield from" does in Python, as I haven't seen it before in that context.
# initializing the data and empty list
data = [1, [2, 3, [4, 5]], 6, [[7], [8, 9]]]
flat_list = []
# function
def flatten_list(data):
# iterating over the data
for element in data:
# checking for list
if type(element) == list:
# calling the same function with current element as new argument
flatten_list(element)
else:
flat_list.append(element)
# flattening the given list
flatten_list(data)
# printing the flat_list
print(flat_list)
(See more at https://geekflare.com/flatten-list-python/)
This question already has answers here:
How do I clone a list so that it doesn't change unexpectedly after assignment?
(24 answers)
Closed 2 years ago.
I would like to fill a dict with lists. But it seems like the dict keeps updating the values i pass in.
I am currently doing something like this:
Dict=dict()
lst = list()
for i in range(10):
lst.append(i)
Dict[f'{i}']=lst
and would like to obtain Dict={'0': [0], 1: [1,2], ... }
but I get Dict={'0': [0, 1, 2, 3, 4, 5, 6, 7, 8, 9], '1': [0, 1, 2, 3, 4, 5, 6, 7, 8, 9], ...}
Is there a nice way to get around this?
Thanks for your help.
Dict[f'{i}']=lst inserts the exact same list object over and over again, because assignment generally doesn't copy objects. lst.append(i) modifies that one list object. So your dictionary ends up containing lots of pointers to the same list.
To insert a different object each time, copy the current list:
Dict[f'{i}']=lst.copy()
# or Dict[str(i)] = lst.copy()
This question already has answers here:
How do I reverse a list or loop over it backwards?
(37 answers)
Closed 2 years ago.
I want to change the first element by the last, the second element by the last but one, etc..
Then I want to print this.
My list: x = [1, 2, 3, 4, 5] I want this list as: y = [5, 4, 3, 2, 1]
how can I do it in Python 3.x?
A simple way is the following:
y = x[::-1]
Use reversed such as ,
x = [1, 2, 3, 4, 5]
print(list(reversed(x)))
output:
[5, 4, 3, 2, 1]
This question already has answers here:
How to extract the n-th elements from a list of tuples
(8 answers)
Closed 3 years ago.
Is there a more efficient way to select items from a raw_list to achieve this
new_list = [raw_list[0][2], raw_list[1][2], raw_list[2][2]]
I was trying to do something like raw_list[:][2], but : could not do what I want.
it is as simple as looping through the list and getting the item at the position 0:
alist = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
newList = []
x=0
while x != len(alist):
print(alist[x][0])
newList.append(alist[x][0])
x = x + 1
This can be done a little more effectively with a for loop, and i suggest you use that, but a while loop is more explanatory.
This question already has answers here:
Pythonic way to combine (interleave, interlace, intertwine) two lists in an alternating fashion?
(26 answers)
Closed 5 years ago.
I have a question:
I have 2 lists:
list_1 = [1, 2, 3]
list_2 = [4, 5, 6]
And I want to merge them in order to have the following result:
mergedlist = [1, 4, 2, 5, 3, 6]
How can I do that?
Like this:
mergedlist = list_1 + list_2
If you want that specific order in mergedlist:
mergedlist = []
for i, entry in enumerate(list_1):
mergedlist.extend([entry, list_2[i]])
You can use chain from iter tools:
list_1 = [1, 2, 3]
list_2 = [4, 5, 6]
from itertools import chain
res = list(chain.from_iterable((list_1[x], list_2[x]) for x in range(len(list_1))))
=> [1, 4, 2, 5, 3, 6]
In my opinion, the most Pythonic way would be:
merged_list = [item for pair in zip(list_1, list_2) for item in pair]
Alternatively, you can use collections.chain as well:
merged_list = list(chain.from_iterable(zip(list_1, list_2)))