This question already has answers here:
How do I make a flat list out of a list of lists?
(34 answers)
Closed 2 years ago.
I would like to remove a list from inside another list. I have a list which looks like this:
[[1, 2, 3, 4, 5, 6]]
I would like the list to look like this, so it is only a singular list rather than nested:
[1, 2, 3, 4, 5, 6]
To achieve the desired result, you can just access the zero index from original list.
a = [[1, 2, 3, 4, 5, 6]]
>>> b = a[0]
>>> b
// [1, 2, 3, 4, 5, 6]
If your list is :
lst = [[1,2,3,4,5,6]]
then you can do like this
new_lst = lst[0]
You new_lst would be :
[1,2,3,4,5,6]
If your original lst has many lists inside it like :
lst = [[1,2,3,4,5,6],[7,8,9,10],...]
Then you can do the following :
new_lst = []
for i in lst :
for j in i :
new_lst.append(j)
So your new_lst would be :
[1,2,3,4,5,6,7,8,9,10...]
You could also do it in a shorthand notation as :
new_list = [element for sublist in lst for element in sublist]
The above snippet is exactly similar as previous one, just a shorthand notation
Related
This question already has answers here:
How do I remove duplicates from a list, while preserving order?
(30 answers)
Closed 1 year ago.
I tried the following code but it doesn't seem to remove any duplicates
list2 = [element for element in list1 if element not in list2]
however,
for i in list1:
if i not in list2:
list2.append(i)
this code works perfectly fine, can anyone please let me know why is this the case?
It's producing an identical list as list2 contains no elements at run-time. What you'd want is this:
list1 = [1, 2, 3, 3, 5, 9, 6, 2, 8, 5, 2, 3, 5, 7, 3, 5, 8]
list2 = []
[list2.append(item) for item in list1 if item not in list2]
print(list2)
This question already has answers here:
Removing duplicates in lists
(56 answers)
Closed 4 years ago.
I have a list (ex: [1, 2, 2, 3, 4, 3]) and I need to only keep the first occurrence of any of the elements in the list (ex: the list should become [1, 2 ,3, 4]). I know that I could do something like this:
badList = [1, 2, 2, 3, 4, 3]
goodList = []
for element in badList:
if element in goodList:
continue
goodList.append(element)
print (goodList)
However, this is a very messy solution, and I am hoping there is a more elegant way.
from collections import OrderedDict
list(OrderedDict.fromkeys(badList))
[1, 2, 3, 4]
credit to #poke
Just convert to a set then back to a list:
goodList = list(set(badList))
This question already has answers here:
How do I split a list into equally-sized chunks?
(66 answers)
Closed 6 years ago.
I need to add the first three elements of a list then add the next three elements of a list and so forth. This is the code I have got so far:
def get_triple_sums_list(a_list):
new_list = []
for numbers in range(0,len(a_list)):
numbers = sum(a_list[:3])
new_list.append(numbers)
return new_list
if a_list == []:
return []
For the list:
[1, 5, 3, 4, 5, 2]
This in turn gives me the result:
[9]
I need to get
[9, 11]
If the remaining numbers is less than 3, it gives me the remainder of the sum ie,
[1, 6, 2, 4, 3]
Gives me
[9, 7]
And
[1, 6, 2, 4]
Give me
[9, 4]
Let's analyze your code!
def get_triple_sums_list(a_list):
new_list = []
for numbers in range(0,len(a_list)):
numbers = sum(a_list[:3]) #You should be using the variable
#numbers here somehow.
#^^^^^^^ - You are overwriting the for-loop index.
new_list.append(numbers)
return new_list #Why are you returning here? You should be
#appending to `new_list`.
if a_list == []:
return []
Here is the fixed code:
def get_triple_sums_list(a_list):
new_list = []
for index in range(0,len(a_list), 3): #Range takes a 3rd param!
total = sum(a_list[index:index+3])#Get all the elements from the
#index to index+3
new_list.append(total)
return new_list
UPDATE: It seems there's a shortening contest going on -- and I do not want to be left behind. Here's an ugly version I'd like to add to the list.
>>> a = [1,2,3,4,5,6,7,8]
>>> a += [0]*(len(a)%3) #For people who are too lazy to import izip_longest
>>> map(sum,zip(a[::3], a[1::3], a[2::3]))
[6, 15, 15]
I like SuperSaiyan's approach of explaining things, I'll be the one who shortens it a bit. You can get the same result with a single comprehension:
l = [1, 5, 3, 4, 5, 2]
n = 3
r = [sum(l[i:i+n]) for i in range(0, len(l), n)]
print(r)
[9, 11]
l[i:i+n] splits the list in even chunks of length 3 and sum takes care of adding these together. Using the for i in range(0, len(l), n) we dictate that this operation is to happen for ceil(len(l) / 3) times.
Just cuz I like to be different.
l = [1, 5, 3, 4, 5, 3, 42]
g = lambda l,s: [sum(l[i:i+s]) for i in range(0,len(l),s)]
print g(l,3)
#>> [9,12,42]
The other answer mentions the fault with your code. However do note that it's always easier to use a list comprehension in these cases.
>>> l = [1, 5, 3, 4, 5, 2]
>>> [sum(l[i:i+3]) for i in range(0,len(l),3)]
[9, 11]
It also works for un-mod-3 lists
>>> l = [1, 5, 3, 4, 5]
>>> [sum(l[i:i+3]) for i in range(0,len(l),3)]
[9, 9]
See What does "list comprehension" mean? How does it work and how can I use it? for more details about a list comprehension.
Here is a slightly different way of doing it using zip_longest from itertools (izip_longest in python2), it splits the list in three lists then zip them to get packs of three elements and finally sums the packs:
from itertools import zip_longest
a=[1, 6, 2, 4, 3]
b=zip_longest(a[0::3],a[1::3],a[2::3],fillvalue=0)
result=[sum(x) for x in b]
>>>[9, 7]
Alternatively, you may achieve it by using map() with lambda function as:
>>> my_list = [1, 5, 3, 4, 5, 2]
>>> list(map(lambda x: sum(my_list[x:x+3]), range(0, len(my_list), 3)))
[9, 11]
This question already has answers here:
How do I make a flat list out of a list of lists?
(34 answers)
Closed 6 years ago.
I have a list of objects:obj_list like below:
obj_1 = SomeObj()
obj_2 = SomeObj()
obj_1.items = [obj10, obj11, obj12]
obj_2.items = [obj20, obj21, obj22]
obj_list = [obj_1, obj_2]
Now I want a list containing all the items like below using comprehension:
[obj10, obj11, obj12, obj20, obj21, obj22]
I have tried as follows:
[item for item in obj.items for obj in obj_list]
>>> class SomeObj:
... pass
...
>>> obj1=SomeObj()
>>> obj1.items=[1, 2, 3]
>>> obj2=SomeObj()
>>> obj2.items=[4, 5, 6]
>>> obj_list=[obj1, obj2]
>>> [obj.items for obj in obj_list]
[[1, 2, 3], [4, 5, 6]]
>>> [item for obj in obj_list for item in obj.items]
[1, 2, 3, 4, 5, 6]
>>> import itertools
>>> list(itertools.chain(*[obj.items for obj in obj_list]))
[1, 2, 3, 4, 5, 6]
im trying to write a fun script for a password generator that DOES NOT have any rational connection to the user, thus making it alot harder to use the fact you know the user in order to guess it.. but im new to python
basically i know how to use append and extend but i was trying to find a way to add a whole list to another, for example is list1 is (1 2 3 4) and list2 is (3 4 5 9) i want to know how to addlist2tolist1` and get a list1: (1 2 3 4 3 4 5 9)
ty!
This can be done very simply using the extend method. The following is a quick demonstration:
>>> l1 = [1,2,3]
>>> l2 = [3,4,5]
>>> l1.extend(l2)
>>> l1
[1, 2, 3, 3, 4, 5]
The addition operator has the same effect:
>>> l1 + l2
[1, 2, 3, 3, 4, 5]
Just use the concatenate overloaded operator:
list1 = [1, 2, 3, 4]
list2 = [3, 4, 5, 9]
list1 + list2
=> [1, 2, 3, 4, 3, 4, 5, 9]
The above will create a new list with the result of concatenating list1 and list2. You can assign it to a new variable if necessary.
You have 2 ways (as far as I know). Let's:
list1 = [1,2,3]
list2 = [4,5,6]
First:
list1 = list1 + list2
print list3
>>> [1,2,3,4,5,6]
Second:
list1.extend(list2)
print list1
>>> [1,2,3,4,5,6]
Also remember that (1,2,3) is a tuple not a list.
Addition concatenates lists:
list1 + list2