Python iterate through a list of tuples [duplicate] - python

This question already has answers here:
Find an element in a list of tuples
(10 answers)
How to check if all elements of a list match a condition?
(5 answers)
Closed 5 years ago.
I am new in Python and I was curious if I can do something like this:
Let's say that I have a list of tuples and one variable like this:
list = [(123,"a"),(125,"b")]
variable = (123,"c")
Is it possible to search for the first element of the variable in the list like this?
if variable[0] in list[0]:

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 to chain individual elements of list into one individual element in Python [duplicate]

This question already has answers here:
How to concatenate (join) items in a list to a single string
(11 answers)
Closed 3 years ago.
I have the following list
List1 =['4','0','1','k']
How do i make sure that individual elements are combined into one single entity?
Here is the desired output
List1 =['401k']
Use str.join:
List1 = [''.join(List1)]

count an element in a list with list inside in python [duplicate]

This question already has answers here:
Nested List and count()
(8 answers)
Closed 4 years ago.
I have a list with one list inside and I would like to count how many times is one element repeat. for example:
list = ['a','b','c',['a','d']]
find = 'a'
list.count(find)
The ouptput is 1, but I'm looking for 2.
There is any easy way to do it?
thanks
Archive with chain.from_iterable
from itertools import chain
print(list(chain.from_iterable(lst)).count('a'))
First make your list flatten and get the count.

How to access List elements with specific multiple index [duplicate]

This question already has answers here:
Pythonic way to return list of every nth item in a larger list
(9 answers)
Closed 4 years ago.
I have a list
list=[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]
I need to store every fifth element, with the result:
result=[5,10,15]
list=[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]
results = list[4::5]
Here you go!

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)])

Categories