Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,995 software developers and data experts.

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use .

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.

python list assign multiple indices

python asp.net asp.net-mvc scala azure

Python assigning multiple variables to same list value? - python

I'm writing a function to calculate calendar dates. While cutting down on lines, I've found that I am unable to assign multiple variables to the same range. Jan, Mar, May, Jul, Aug, Oct, Dec = range(1,32) Would there be an efficient way to assign these values and why does python give a ValueError?

Use jan = mar = may = ... = range(1, 32), the easiest way to do what you described would be to use the x=y=z ... = value syntax, where x, y, z, and any other variables you include will all be assigned the value of value. in your example, all the comma-separated variables on the left side of the equals sign are assigned to sequential values of a tuple on the right side. hence, you could do something like this: values = ( 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31) jan, feb, mar, apr, may, jun, jul, aug, sep, oct, nov, dec = values in your code, you have 7 values on the left, and 31 on the right, so you get an out of range error because the list on the left is longer than the number or variables on the left side to be assigned the values in it. i know the code above doesn't have much relevance to achieving your goal, but i thought i'd at least give some insight as to what it was trying to do. :d, python dictionary sort keys by a value with timestamp, normalizing timestamps in different formats to a common format, need to grab a 3x3 neighbourhood of an input cell from a 2d numpy array, python lambda if statements, python - for cycle in a list of lists [duplicate].

Copyright © 2018 www.develop-bugs.com

Hello, buddy!

I'm a coder. Welcome to my blog. Here are some of the records on my job.

Python assigning multiple variables to the same list value?.

I'm writing a function to calculate calendar dates. While cutting down on lines, I've found that I am unable to assign multiple variables to the same range .

Would there be an efficient way to assign these values and why does python give a ValueError?

Related Articles

How to assign multiple variables of the same value in a smart way?, more elegant way to declare multiple variables at the same time, assign two variables to the same value with an expression?, sql - filling multiple variables in the same query, assigning multiple variables to the inspector, how can i pass multiple variables at the same time on this script, echo multiple variables in the same bash line, php set assigning multiple keys to the same value, google maps api v3 - assign multiple listeners to the same marker object, using the wherein method to return multiple results for the same table value but at a different index, python - check if multiple variables have the same value, multiplication element by element in an array of data - multiple variables at the same time (r), python: set many variables to the same value - with the automatic number of variables, do recursive methods create multiple variables of the same name?.

Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

python - read from file and assign into multiple lists

How do I make the code so it reads the file and splits it so that it can assign which lines I specify into a certain named list. I want the first 3 lines to be assigned into a list then the next 3 and so on, so it would be:

list1 = Steve, Cat, v, 2, 3, 4, 1, 28

list2 = John, Dog, h , 6, 1, 7, 2, 45

list3 = (something)

text file below named character:

main code below

Thanks for the help I'm new to python

Infusion's user avatar

As mentioned in the comments, using a with block will close the file automatically once it goes out of scope.

This solution grabs your lines in blocks of 3. It then places the lists into a dictionary with the name as the key. It's not practical to put the lists into variables with the names as you have done.

As S3DEV has kindly mentioned in the comments, this is not very effective for large files and the file object itself can be iterated rather than reading in the entire file with readlines() . I've taken a shortcut and assumed that your file will always have 3 lines for each grouping and that your file will not end in the middle of an entry.

(note: walrus operator := requires Python 3.8 or above)

jonsca's user avatar

Your Answer

Sign up or log in, post as a guest.

Required, but never shown

By clicking “Post Your Answer”, you agree to our terms of service , privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged python list loops for-loop readline or ask your own question .

Hot Network Questions

python list assign multiple indices

Your privacy

By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy .

Python List Comprehension: assign to multiple variables

Is there a way to assign to multiple variables in a one-liner?

Let's say I have a list of 3D points and I want x, y and z lists.

I am thinking of something like:

You can use zip() function:

Or if you want lists instead of tuples:

Collected from the Internet

Please contact [email protected] to delete if infringement.

Click to generate QR

python list comprehension and multiple variables

Multiple variables in list comprehension, python multiple list comprehension, python list comprehension with multiple 'if's, multiple conditions for list comprehension in python, python list comprehension with multiple loops, python: list comprehension with multiple conditions, list comprehension in python with multiple if statements, list comprehension with multiple regex with python, python list comprehension with multiple conditions, how to add multiple variables in list comprehension, enumerating three variables in python list comprehension, placing variables inside a python list comprehension, assign values with comprehension list based on list of tuples in base python, python list comprehension - replacing multiple letters in a string, python list comprehension loop over multiple collections, python list comprehension - generating multiple data, python - applying multiple conditions output in list comprehension, python dictionary comprehension: assign value to key, where value is a list, how does this code of list comprehension with multiple variables assigned works, python list comprehension: populating each inner-list with two variables, python assign multiple variables with map function, assign multiple variables in a collective string - python, how to assign serial data to multiple list of variables and averaged them for a specify duration in python, list comprehension with set comprehension in python, multiple list comprehension haskell, list comprehension with multiple arguments, multiple loop in list comprehension, related  related.

IMAGES

  1. list slicing in python

    python list assign multiple indices

  2. Python List

    python list assign multiple indices

  3. Python List

    python list assign multiple indices

  4. Python Lists: Indexing & Slicing

    python list assign multiple indices

  5. Python List Index

    python list assign multiple indices

  6. Index Of Element In List Python

    python list assign multiple indices

VIDEO

  1. Olympiad Mathematics Exponential problem. You should know how to solve this Olympiad question

  2. (Tip 6) Python Added A New Way To Assign With Walruses

  3. Log Exponential| Hard Math| Olympiad| Dare to solve| Olympiad Exponential Math| A must watched

  4. Python List

  5. Configuring Catalogs in Product Experience Manager (EP PXM)

  6. INDEX NUMBER IN PYTHON #pythontutorial #python #shorts #youtubeshorts

COMMENTS

  1. Determine multiple indices within a list

    I am trying to return a list of indices for elements within another list that pass some sort of rule (as fast as possible). I am interested in the Elementspassed matrix just to be a list of indices in which

  2. What is the best way in python to assign each of the same element of one list to the same

    Join Stack Overflow to learn, share knowledge, and build your career. Sign up with email. Sign up with GitHub. Sign up with Facebook

  3. Assigning multiple python list elements in one go

    Being familiar with the MATLAB programming language I'm looking for a convenient way to achieve the following assignment in python. A copy of the original list would also be fine

  4. Python Part IV

    Because of pitfalls like this, code which modifies data in place can be more difficult to understand. However, it is often far more efficient to modify a large data structure in place than to create a modified copy for every small change

  5. Assigning multiple python list elements in one go

    Being familiar with the MATLAB programming language I'm looking for a convenient way to achieve the following assignment in python

  6. Python assigning multiple variables to same list value?

    While cutting down on lines, I've found that I am unable to assign multiple variables to the same range. Jan, Mar, May, Jul, Aug, Oct, Dec = range(1,32) Would there be an efficient way to assign these values and why does python give a ValueError?

  7. Python assigning multiple variables to the same list value?

    While cutting down on lines, I've found that I am unable to assign multiple variables to the same range. Python assigning multiple variables to the same list value?advertisements

  8. python

    How do I make the code so it reads the file and splits it so that it can assign which lines I specify into a certain named list

  9. Python List Comprehension: assign to multiple variables

    Is there a way to assign to multiple variables in a one-liner? Let's say I have a list of 3D points and I want x, y and z lists