Ex:
a = []
a.append('Hello')
Which will give the output: ['Hello']
Could you please tell me How to do the same using list comprehension?
You actually do not need to use list comprehension here. Just simply do:
a=['Hello']
You can do it by doing
a = ['Hello' for i in range(1)]
But it will just lead to more memory allocation than needed.
If you only have
a = []
a.append('Hello')
as you know, it would be most practical to simply do
a = ['Hello']
But if you want to append multiple 'Hello's to the list, you can do it like
a = ['Hello' for _ in range(amount)]
Where the amount variable should be replaced with the number of 'Hello's you want in the list.
Another way to do the above is
a = ['Hello'] * amount
Related
I have a variable = 'P13804'
I also have a list like this:
['1T9G\tA\t2.9\tP11310\t241279.81', '1T9G\tS\t2.9\tP38117\t241279.81', '1T9G\tD\t2.9\tP11310\t241279.81', '1T9G\tB\t2.9\tP11310\t241279.81', '1T9G\tR\t2.9\tP13804\t241279.81', '1T9G\tC\t2.9\tP11310\t241279.81']
You can see, if you split each item in this list up by tab, that the third item in each sub-list of this list is sometimes 'P11310' and sometimes is 'P13804'.
I want to remove the items from the list, where the third item does not match my variable of interest (i.e. in this case P13804).
I know a way to do this is:
var = 'P13804'
new_list = []
for each_item in list1:
split_each_item = each_item.split('\t')
if split_each_item[3] != var:
new_list.append(each_item)
print(new_list)
In reality, the lists are really long, and i have a lot of variables to check. So I'm wondering does someone have a faster way of doing this?
It is generally more efficient in Python to build a list with a comprehension than repeatedly appending to it. So I would use:
var = 'P13804'
new_list = [i for i in list1 if i.split('\t')[2] == var]
According to timeit, it saves more or less 20% of the elapsed time.
I have the following code:
s = (f'{item["Num"]}')
my_list = []
my_list.append(s)
print(my_list)
As you can see i want this to form a list that i will then be able to store under my_list, the output from my code looks like this (this is a sample from around 2000 different values)
['01849']
['01852']
['01866']
['01883']
etc...
This is not what i had in mind, i want it to look like this
[`01849', '01852', '01866', '01883']
Has anyone got any suggestions on what i do wrong when i create the list? Thanks
You can fix your problem and represent this compactly with a list comprehension. Assuming your collection is called items, it can be represented as such, without the loop:
my_list = [f'{item["Num"]}' for item in items]
You should first initialize a list here, and then use a for-loop to populate it. So:
my_list = []
for values in range(0, #length of your list):
s = (f'{item["Num"]}')
my_list.append(s)
print(my_list)
Even better, you can also use a list comprehension for this:
my_list = [(f'{item["Num"]}') for values in range(0, #length of your list)]
Suppose a list in Python -> mylist:
mylist = []
I want to now take input from users and update the values afterward in mylist:
input1 = ["xyz","wyh",34]
print mylist
Output: [["xyz","wyh",34]]
input2 = ["yo","hey",657]
print mylist
output: [["xyz","wyh",34],["yo","hey",657]]
append() only works when list has some values already.
I'd advise you take a look through here to see what exactly you need. It sounds like what you want is list.append() however.
mylist = [["X","Y",12], ["A","B",23]]
mylist.append(["L","M",56])
you can use list.append
mylist.append(["L","M",56])
(or)
mylist+[["L","M",56]]
Someone please help me with this code:
agerlist = for agern in anime.info['Genres']:
print agern['name']
Is there any way to save the output into one variable? An example would be: alist = agern in anime.info ?
You mean like this?
alist = [ x['name'] for x in anime.info['Genre']]
Create an empty list and then append the results of for loop to that list or you could use list comprehension.
l = []
for agern in anime.info['Genres']:
l.append(agern['name'])
print l
you could also store that into a generator function, something like this:
for agern in anime.info['Genres']:
yield agern['name']
so that you will actually compute this thing only on request, i.e. during a for cycle, so you will not store in memory each agern['name'] two times.
Is there a Pythonic way to change the type of every object in a list?
For example, I have a list of objects Queries.
Is there a fancy way to change that list to a list of strings?
e.g.
lst = [<Queries: abc>, <Queries: def>]
would be changed to
lst = ['abc', 'def']
When str() is used on a Queries object, the strings I get are the ones in the second code sample above, which is what I would like.
Or do I just have to loop through the list?
Many thanks for any advice.
newlst = [str(x) for x in lst]
You could use a list comprehension.
Try this:
new_list = map(str, lst)
Try this (Python 3):
new_list = list(map(str, lst))
or
new_list = [str(q) for q in lst]