Making a multidimensional list from a one dimensional list [duplicate] - python

This question already has answers here:
How do I split a list into equally-sized chunks?
(66 answers)
Closed 5 years ago.
I know the title is a bit confusing but I couldn't think of a better way of describing it. This is python code.
Say I have a list a=["blue","yellow", "red", "green"]. How would I split it so it looks like this: a=[["blue","yellow"],["red","green"]]?

Just make appropriate slices and put them in a list of their own:
a = [a[:2], a[2:]]
If you don't know the length of the list beforehand, you can use a comprehension:
chunksize = 2
a = [a[i:i+chunksize] for i in range(0, len(a), chunksize)]

Related

Split list element into lists in python [duplicate]

This question already has answers here:
Apply function to each element of a list
(4 answers)
Python Splitting Array of Strings
(6 answers)
Split each string in list and store in multiple arrays
(4 answers)
Closed 1 year ago.
I have a list
list = ['a:b:c:d', 'e:f:g:h', 'i:j:k:l']
What I'm trying to do is make a list of each of it's elements. Something like this:
listelement[0] = list[0].split(':')
print(listelement[0][1])
output: b
I can't seem to figure out how to create something that works similarly to this.
You can try list comprehension to do what you want.
new_list = [element.split(':') for element in list].
Also I would advise against the use of list as a name, since it's a reserved word.

How can I split a list into smaller lists [duplicate]

This question already has answers here:
How can I find same values in a list and group together a new list?
(6 answers)
Closed 2 years ago.
I have a list like so:
[10,10,10,20,20,20,20,30,40,40,40]
I want to split into X amount of lists, where X = how many unique elements there are, in the case above there are 4. So I would want 4 lists like so:
[[10,10,10],[20,20,20,20],[30],[40,40,40]]
Might be a dumb question and there is an easy way to do this but any help is appreciated, language is python3.
itertools.groupby does what you need, except it returns iterators instead of lists. Converting to lists is easy though:
[list(g) for _, g in itertools.groupby(my_list)]

Getting elements (tuples) by group of 2 from a list [duplicate]

This question already has answers here:
How do I split a list into equally-sized chunks?
(66 answers)
Closed 6 years ago.
I have a list containing some coordinates ordered as tuples:
list = [(316852,4.99246e+06), (316858,4.99244e+06), (316880,4.99246e+06), (316863,4.99248e+06), (316852,4.99246e+06)]
and I would like to get its elements by group of 2. The result expected is something like this:
(316852,4.99246e+06), (316858,4.99244e+06)
(316858,4.99244e+06), (316880,4.99246e+06)
(316880,4.99246e+06), (316863,4.99248e+06)
(316863,4.99248e+06), (316852,4.99246e+06)
Any idea on how to obtaining this?
Something like this?
list = [(316852,4.99246e+06), (316858,4.99244e+06), (316880,4.99246e+06), (316863,4.99248e+06), (316852,4.99246e+06)]
for x in range(0, len(list)-1):
print(list[x], list[x+1])
you can try this :
print([list[i:i+2] for i in range(0,len(list),2)])

How to pass the list starting from nth element [duplicate]

This question already has answers here:
Understanding slicing
(38 answers)
Closed 8 years ago.
Suppose I have a list [1,2,3,4,5]. Now I want to pass the elements of this list, starting from 3rd element to a method.
i.e. i want to invoke:
myfunction([3,4,5])
How can I do this in python. tried passing mylist[2], but it doesn't quite work this way it seems.
slicing.
mylist = range(1,6)
>> [1,2,3,4,5]
myfunction(mylist[2:])
>> [3,4,5]

Python list of lists to list [duplicate]

This question already has answers here:
Closed 10 years ago.
I have a fairly simple question (I think).
I have a list of lists in python, and the elements are strings. I wish to have a single list, with elements that are floats.
For example:
lst= [['0.0375'], ['-0.1652'], ['0.1841'], ['-0.0304'], ['0.0211'], ['0.1580'], ['-0.0252'], ['-0.0080'], ['-0.0915'], ['0.1208']]
And I need to have something like:
lst= [0.0375, -0.1652, 0.1841, -0.0304, 0.0211, 0.1580, -0.0252, -0.0080, -0.0915, 0.1208]
[float(x) for (x,) in your_list]

Categories