I have a 2 Dimensional list like that :
list = [[2, 3, 5], [1,2,3], [4,5,6], [8,9,10],[5,6,7]]
I can print the first value of every list with this:
[i[0] for i in list]
and outcome is:
list = [2, 1, 4, 8, 5]
but i want to have outcome like this :
list = [[2,3,5],[1,2,3],[4,5,6]]
my code is this :
new_list = []
for i in list:
row = 1
row_list = list[row]
new_list.append(row_list)
can anyone help me?
I am a bit confused what are you asking, but if I get i right try
print(list[1][1]) #print 2nd element in 2nd subset
print(list[0:3]) #print first 3 elements (in this case subsets) in the list
I hope it help.
To remove few object from list, you can use
list.remove(something) #remove element from list
or to create new list just use
l=list[0:3]
but i want to have outcome like this : list = [[2,3,5],[1,2,3],[4,5,6]]
This should do it:
list_subset = list[:3] # the first 3 elements in the list
you can slice your list like this:
n = 3 # if you have number of items you need
new_list = list[:n]
or:
n = 2 # if you have number of items you want to remove
new_list = list[:-n]
note that:
DO NOT use list as a name of a variable, list is a built-in in python.
Simple slicing can be used to skip last two rows like this:
list = [[2, 3, 5], [1,2,3], [4,5,6], [8,9,10],[5,6,7]]
print(list[:-2])
[[2, 3, 5], [1, 2, 3], [4, 5, 6]]
Related
Suppose, I have a list [0.5,1,1.5,2,2.5,3,3.5,4,4.5], now I would like to extract the indices of a list [1.5,2.5,3.5,4.5], which is a subset of that list.
You can use the inbuilt function <list>.index to find the index of each element(of second list) in the first list.
Having that learnt, you can use list comprehension to achieve what you want:
>>> list1 = [0.5,1,1.5,2,2.5,3,3.5,4,4.5]
>>> list2 = [1.5,2.5,3.5,4.5]
>>> [list1.index(elem) for elem in list2]
[2, 4, 6, 8]
One other option is to use enumerate function. See the following answer:
a = [0.5,1,1.5,2,2.5,3,3.5,4,4.5]
b = [1.5,2.5,3.5,4.5]
indexes = []
for id, i in enumerate(a):
if i in b:
indexes.append(id)
print(indexes)
The output is going to be [2, 4, 6, 8].
I know there are a lot of questions like this one, but I haven't found my answer so far.
I am trying to dynamically fill a list with other lists, but I don't know why my code doesn't do what I want.
My code:
x = [1,2,3]
y = [4,5,6]
x.append(y)
print (x)
What I get:
[1,2,3[4,5,6]]
What I realy want:
[[1,2,3],[4,5,6]]
My goal would be, to dynamically add more dimensions arranged like this.
Can somebody tell me, what I'm doing wrong?
Thanks a lot.
In your example, x is a list containing three elements (integers).
With append, you add a new element. By appending y, you are adding a list as a fourth element (3 integers, one list).
If you want to create a list of lists, tread x and y as elements for that, and combine them in a list:
x = [1,2,3]
y = [4,5,6]
list_of_lists = [x,y]
list_of_lists will then be [[1, 2, 3], [4, 5, 6]].
You can add another list by appending them:
list_of_lists.append([7,8,9])
... which will result in list_of_lists being [[1, 2, 3], [4, 5, 6], [7, 8, 9]].
What you want is create a list of lists. You can do:
x = [1,2,3]
y = [4,5,6]
l = [x,y]
print(l)
Actually, if you want to deal with multi-dimensional arrays, you should probably look at the numpy library (https://numpy.org/)
x = [1,2,3]
y=[22,33,9]
x=(x,y)
print (list(x))
>> [[1, 2, 3], [22, 33, 9]]
x=list(x) #Perform type conversion of the output 'x' to get in list type
x.append([13,32,12])#then append the data you want
print (x)
>> [[1, 2, 3], [22, 33, 9], [13, 32, 12]]
I would like a one line way of assigning two variables to two different values in a for loop.
I have a list of list of values
list_values = [[1, 2, 3], [4, 5, 6]]
I have tried to do this, and it works but is not pythony:
first = [i[0] for i in list_values]
second = [i[1] for i in list_values]
Which makes:
first = [1, 4]
second = [2, 5]
I want to write something like:
first, second = [i[0]; i[1] for i in list_values]
Is something like this possible?
You could use the zip() function instead:
first, second = zip(*list_values)[:2]
or the Python 3 equivalent:
from itertools import islice
first, second = islice(zip(*list_values), 2)
zip() pairs up elements from the input lists into a sequence of new tuples; you only need the first two, so you slice the result.
list_values = [[1, 2, 3], [4, 5, 6]]
first, second = [[i[0], i[1]] for i in list_values]
Next time use something other than the "i", like "elem" etc.
Let's say I have 2 arrays inside a single array, like:
main_array = [[1, 2, 3, 4], [4, 5, 6, 7]]
I would like to find the min and max of each of those arrays and store them in a single array. For the above it would be:
result = [1, 4, 4, 7]
How do I use Python's inbuilt min() and max() in this case?
I tried min(main_array) and max(main_array) but that is giving me:
result = [1,7]
You can just use min() or max() on single list to get it's min/max value. You can also use list comprehension to loop through lists in list and functions you want to use:
main_array = [[1,2,3,4], [4,5,6,7]]
res = [func(l) for l in main_array for func in (min, max)]
print(res)
main_array = [[1, 2, 3, 4], [4, 5, 6, 7]]
result = []
for inner_list in main_array:
result.append(min(inner_list))
result.append(max(inner_list))
Probably the more readable way to do it is:
result = []
for list in main_array:
result.append(min(list))
result.append(max(list))
print(result)
Try
main_array = [[1,2,3,4],[4,5,6,7]]
out = []
for arr in main_array:
out.extend([min(arr), max(arr)])
print(out)
You need to iterate over each of the sub arrays and call min and max on that.
You could also use generators
def minthenmax(arr):
for i in arr:
yield min(i)
yield max(i)
print(list(minthenmax(main_array)))
First off you're missing a common on the definition of your main array...
main_array = [[1,2,3,4],[4,5,6,7]]
To get the min of the first sub-array.
min(main_array[0])
...and the max...
max(main_array[0])
Hopefully you can work out the rest from this.
In one line. You can use build-in map function to map two inner lists to min function and then max function. Finally you can concatenate them.
map(min, main_array) + map(max, main_array)
main_array = [[1,2,3,4],[4,5,6,7]]
x = min(main_array[0]), max(main_array[0])
y = min(main_array[1]), max(main_array[1])
new_array = [i for i in x + y]
print (new_array)
output:
[1, 4, 4, 7]
You've got to search for min and max on each item of lst.
lst = [[1,2,3,4],[4,5,6,7]]
m = []
for k in lst: m.append([min(k), max(k)])
=> [[1, 4], [4, 7]]
I know we can merge two lists by using something like final_list= list1 + list2 but if the lists are generated by a python code and they don't have a variable associated with them like list1 and list2, how can we merge them? Say, my code does something like print output to give:
[1,2,3,4]
[2,0,5,6]
I'd like to merge them so I can get unique values using set(final_list). But how do I get the final_list?
PS- My code can return multiple lists. It is not restricted to two.
def somefunc(param):
#does something
return alist,blist
my_alist,my_blist = somefunc(myparam)
print my_alist, my_blist
#prints both lists.
When you return multiple values from a function they are returned in a tuple. You can easily unpack the tuple
You can either modify the function which is generating output, or the harder way being you manually convert it into a string and then into a set.
list = []
strings_of_list = output.split('\n')
for string in strings_of_list:
values = string[1:-1].split(',')
for val in values:
list+=[int(val)]
set(list)
Assign a variable to a function. Taking the lists the function generated, join them together in another variable. Just make sure that your function returns the generated list, and doesn't just print it out.
# my_list_generator returns two values.
>>> a, b = my_list_generator()
>>> a
[1, 2, 3, 4]
>>> b
[2, 0, 5, 6]
>>> final_list = a + b
>>> final_list
[1, 2, 3, 4, 2, 0, 5, 6]
Cross all that out! Now that I know the function can return multiple objects, let do this (with a little list comprehension):
lists = [i for i in my_list_generator()]
# lists might look like [[1, 2, 3, 4], [2, 0, 5, 6]]
# And now use a for loop to get each value
final_list = []
for sublist in lists:
final_list.extend(sublist)
# final_list will look like [1,2,3,4,2,0,5,6]
Also, if you don't want duplicates, just do one more thing:
real_final_list = [i for i in final_list if i not in real_final_list]
If I understand correctly:
You have a function (let's call it listGen() for now) which returns some number of lists. Now, you want to put these list together into one big list, final_list.
You could do the following:
# listGen defined earlier
final_list = []
for i in listGen():
final_list += i
unique_values = set(final_list) # or whatever you wanted to do with it
Since listGen returns a tuple, we can loop over its contents, those being the lists you want to append to each other.