TypeError: list indices must be integers or slices, not tuple for list of tuples

2024/11/19 20:07:01

I am getting "list indices must be integers or slices, not tuple" error while trying to generate list from list of tuples. list of tuples have the following structure:

[(29208, 8, 8, 8), (29209, 8, 8, 8), (29210, 8, 8, 8), (29211, 8, 8, 8)]

The first element in the tuple is a time series, other elements are state of some variables.

The loop for converting from list of tuples to simple list is following:

TimeAxis = []for n in lst:TimeAxis.append(lst[n][0])

Where lst has format as described above. For some reason it throws an error:

Traceback (most recent call last):File "X:\Temp\XXX_python_graph\RTT_Plot.py", line 30, in <module>Time.append(lst[n][0])
TypeError: list indices must be integers or slices, not tuple

I understand this is novice question, but other solutions on stackoverflow do not work. Thanks in advance.

Answer

Python's for loop is a Foreach construct; you iterate over the elements of the list, not an index.

So n is one of the tuples from lst, not an index. Use it directly:

for n in lst:TimeAxis.append(n[0])

You could simplify your code by using a list comprehension:

TimeAxis = [tup[0] for tup in lst]
https://en.xdnf.cn/q/118481.html

Related Q&A

Spark Unique pair in cartesian product

I have this:In [1]:a = sc.parallelize([a,b,c]) In [2]:a.cartesian(a).collect() Out[3]: [(a, a), (a, b), (a, c), (b, a), (c, a), (b, b), (b, c), (c, b), (c, c)]I want the following result:In [1]:a = sc.…

How to use double click bid manager(DBM) API in python

I am trying to use the google Double click bid manager (DBM) API, to download reports, I am trying to make this automatic without manual authentication, but all I can find is the GitHub repo for DBM sa…

How can I replace a value in an existing excel csv file using a python program?

How can I update a value in an existing .csv file using a python program. At the moment the file is read into the program but I need to be able to change this value using my program, and for the change…

Why might Python break down halfway through a loop? TypeError: __getitem__

The GoalI have a directory with 65 .txt files, which I am parsing, one by one, and saving the outputs into 65 corresponding .txt files. I then plan to concatenate them, but Im not sure if jumping strai…

RoboBrowser getting type error NoneType object is not subscriptable

Im trying to make a kahoot spammer which inputs a pin number and a username, decided by the user. Im getting a type error when I run this code:import re from robobrowser import RoboBrowser#Getting pin …

How to create a 2d list with all same values but can alter multiple elements within? (python)

Im trying to create a list that holds this exact format: [[2],[2],[2],[2],[2],[2],[2],[2],[2],[2]]and when list[3][0] = 9 is called, the list becomes [[2],[9],[2],[9],[2],[9],[2],[9],[2],[9]]How do I c…

Understanding Function Closures [duplicate]

This question already has answers here:Why arent python nested functions called closures?(10 answers)Closed 9 years ago.Im struggling to understand Function closures properly. For example in the code …

Update value for every row based on either of two previous columns

I am researching ATP Tour male tennis data. Currently, I have a Pandas dataframe that contains ~60,000 matches. Every row contains information / statistics about the match, split between the winner and…

Count consecutive equal values in array [duplicate]

This question already has answers here:Count consecutive occurences of values varying in length in a numpy array(5 answers)Closed 5 years ago.Say I have the following numpy array:a = np.array([1,5,5,2,…

how can I show please wait gif image before the process is complete

I want to show "please wait gif" image from img() class before the ListApp() class process is complete and then as soon as the process of that class is completed the screeen of ListApp should…